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

时间:2013-10-29 11:40:59

标签: java android xml

<?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>

每当执行此代码时:

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

我收到此错误:

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.

怎么可能?

我有

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

4 个答案:

答案 0 :(得分:4)

将这些属性添加到imageview的

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

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

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

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

检查链接以获取更多信息

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

答案 1 :(得分:2)

android:layout_widthandroid:layout_height添加到ImageView

像这样

<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>

答案 2 :(得分:0)

如果遇到这个问题。你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>

所以,我的代码警告那些 [08-11 18:32:33.525:E / AndroidRuntime(2306):java.lang.RuntimeException:二进制XML文件行#27:您必须提供layout_width属性。]

答案 3 :(得分:0)

此处给出的问题直接导致XML出现问题。如果将主题应用于UI元素并且主题未定义尺寸(此处为layout_width),也会发生此错误。例如。这是我构造AlertDialog并在样式文件中定义的情况:

  <style name="FooBar.DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorPrimary</item>
    </style>

<style name="FooBar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="alertDialogTheme">@style/FooBar.DialogTheme</item>
        <!-- ... -->
    </style>

添加:

  <style name="FooBar.DialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

解决了这个问题。