Java 二进制 XML 文件第 7 行:您必须提供 layout_width 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19657255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:11:58  来源:igfitidea点击:

Binary XML file line #7: You must supply a layout_width attribute

javaandroidxml

提问by Elad Benda

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/off_background">

<ImageView 
    android:id="@+id/bottom_layer" 
    android:src="@drawable/handle"/>

<ImageView
    android:id="@+id/upper_layer"
    android:src="@drawable/switch_v"/>

</FrameLayout>

whenever this code is executed:

每当执行此代码时:

    inflater.inflate(R.layout.settings_switch, this);

I get this error:

我收到此错误:

10-29 13:27:00.090: E/AndroidRuntime(22364): Caused by: java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.

10-29 13:27:00.090: E/AndroidRuntime(22364): Caused by: java.lang.RuntimeException: Binary XML file line #7: You must supply a layout_width attribute.

how can it be?

怎么会这样?

I have

我有

  android:layout_width="match_parent"
    android:layout_height="match_parent" 

采纳答案by Raghunandan

Add these attributes to imageview's

将这些属性添加到 imageview 的

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  // can use dp like 20dp instead of wrap_content

All view groups include a width and height (layout_width and layout_height), and each view is required to define them.

所有视图组都包含宽度和高度(layout_width 和 layout_height),每个视图都需要定义它们

wrap_contenttells your view to size itself to the dimensions required by its content fill_parent(renamed match_parent in API Level 8) tells your view to become as big as its parent view group will allow.

wrap_content告诉您的视图将自身大小调整为其内容所需的尺寸 fill_parent在 API 级别 8 中重命名 match_parent)告诉您的视图变得与其父视图组允许的一样大。

In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or fill_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes. The accepted measurement types are defined in the Available Resources document.

通常,不建议使用像素等绝对单位指定布局宽度和高度。相反,使用相对测量值(例如与密度无关的像素单位 (dp)、wrap_content 或 fill_parent)是一种更好的方法,因为它有助于确保您的应用程序能够在各种设备屏幕尺寸上正确显示。可接受的测量类型在可用资源文档中定义。

Check the link for more info

检查链接以获取更多信息

http://developer.android.com/guide/topics/ui/declaring-layout.html

http://developer.android.com/guide/topics/ui/declaring-layout.html

回答by morroko

Add android:layout_widthand android:layout_heightto both ImageView's

android:layout_width和添加android:layout_height到两者ImageView

like this

像这样

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/off_background">

<ImageView 
    android:id="@+id/bottom_layer" 
    android:src="@drawable/handle"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:id="@+id/upper_layer"
    android:src="@drawable/switch_v"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

回答by Savino

If you meet this problem. you ImageView\TextView\Button. they lack of the attribute layout_with or layout_height. i met this problems when i modified my values/styles.xml/. Because i removed the attribute of android:layout_width. The original: <style name="blue_btn_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">45dip</item> <item name="android:layout_marginLeft">15dip</item> <item name="android:layout_marginRight">15dip</item> <item name="android:layout_marginTop">30dip</item> <item name="android:layout_marginBottom">30dip</item> <item name="android:background">@drawable/blue_btn_selector</item> <item name="android:textColor">@android:color/white</item> <item name="android:textSize">@dimen/btn_text_size</item> <item name="android:gravity">center</item> </style>what i removed the attribute.

如果遇到这个问题。你 ImageView\TextView\Button。它们缺少 layout_with 或 layout_height 属性。我在修改 values/styles.xml/ 时遇到了这个问题。因为我删除了 android:layout_width 的属性。原文: <style name="blue_btn_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">45dip</item> <item name="android:layout_marginLeft">15dip</item> <item name="android:layout_marginRight">15dip</item> <item name="android:layout_marginTop">30dip</item> <item name="android:layout_marginBottom">30dip</item> <item name="android:background">@drawable/blue_btn_selector</item> <item name="android:textColor">@android:color/white</item> <item name="android:textSize">@dimen/btn_text_size</item> <item name="android:gravity">center</item> </style>我删除了什么属性。

<style name="blue_btn_style">
    <item name="android:background">@drawable/blue_btn_selector</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">@dimen/btn_text_size</item>
    <item name="android:gravity">center</item>
</style>

so,my code is warning those [08-11 18:32:33.525: E/AndroidRuntime(2306): java.lang.RuntimeException: Binary XML file line #27: You must supply a layout_width attribute.]

所以,我的代码警告那些 [08-11 18:32:33.525: E/AndroidRuntime(2306): java.lang.RuntimeException: Binary XML file line #27: You must provide a layout_width 属性。]