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

4 回答 4

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

于 2013-10-29T11:42:18.047 回答
2

android:layout_width将and添加到android: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>
于 2013-10-29T11:44:00.393 回答
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: Binary XML file line #27: You must provide a layout_width attribute。]

于 2016-08-11T10:47:58.383 回答
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>

解决了这个问题。

于 2020-11-12T03:44:46.960 回答