【问题标题】:Binary XML file line #7: You must supply a layout_width attribute二进制 XML 文件第 7 行:您必须提供 layout_width 属性
【发布时间】:2013-10-29 11:40:58
【问题描述】:
<?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" 

【问题讨论】:

  • 另一种情况你会发现这个错误是:dimens.xml中为sw720dp定义了维度,但是对于低于sw720dp的设备没有定义,你使用这个dimen var来设置layout_width,并且它会在小于 sw720dp 的设备上崩溃

标签: java android xml


【解决方案1】:

将这些属性添加到 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

【讨论】:

    【解决方案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>
    

    【讨论】:

    • 第一个图像视图将填满屏幕,因为它有 match_parent
    • 是的,如果你想要图像将只分配空间到它的大小然后使用 wrap_content 否则想要全屏的图像将它给 match_parent
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      这里给出的问题直接导致 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>
      

      解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        • 2018-07-30
        相关资源
        最近更新 更多