Java 二进制XML文件第7行:必须提供布局宽度属性
我得到这个错误:Java 二进制XML文件第7行:必须提供布局宽度属性,java,android,xml,Java,Android,Xml,我得到这个错误: 10-29 13:27:00.090:E/AndroidRuntime(22364):原因:java.lang.RuntimeException:二进制XML文件行#7:必须提供布局宽度属性。 怎么可能呢 我有 inflater.inflate(R.layout.settings_switch, this); 将这些属性添加到imageview的 android:layout_width="match_parent" android:layout_heig
10-29 13:27:00.090:E/AndroidRuntime(22364):原因:java.lang.RuntimeException:二进制XML文件行#7:必须提供布局宽度属性。
怎么可能呢
我有
inflater.inflate(R.layout.settings_switch, this);
将这些属性添加到imageview的
android:layout_width="match_parent"
android:layout_height="match_parent"
所有视图组都包含一个宽度和高度(布局宽度和布局高度),每个视图都需要定义它们
wrap_content
告诉视图将自身大小调整为其内容所需的尺寸
fill\u parent
(API级别8中重命名的match\u parent)告诉您的视图变大到其父视图组允许的大小
通常,不建议使用绝对单位(如像素)指定布局宽度和高度。相反,使用相对测量,如密度独立像素单位(dp)、换行内容或填充父项,是一种更好的方法,因为它有助于确保应用程序在各种设备屏幕大小上正确显示。可接受的测量类型在可用资源文档中定义
查看链接了解更多信息
将
android:layout\u width
和android:layout\u height
添加到ImageView
中
像这样
android:layout_width="wrap_content"
android:layout_height="wrap_content"
// can use dp like 20dp instead of wrap_content
如果遇到此问题。您可以单击ImageView\TextView\按钮。它们缺少属性LAYOU with或LAYOU height。 我在修改value/styles.xml/时遇到了这个问题。因为我删除了android的属性:layout\u width。 原件:
匹配父项
45度
15浸
15浸
30度
30度
@可牵引/蓝色btn选择器
@android:彩色/白色
@尺寸/字体尺寸
居中
我删除了属性
<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>
@可牵引/蓝色btn选择器
@android:彩色/白色
@尺寸/字体尺寸
居中
所以,我的代码是警告那些
[08-11 18:32:33.525:E/AndroidRuntime(2306):java.lang.RuntimeException:二进制XML文件行#27:必须提供布局宽度属性。]此处给出的问题直接导致XML中导致问题的行。如果将主题应用于UI元素,且主题未定义维度(此处为
布局\u宽度
),则也可能发生此错误。例如,我在构造一个AlertDialog
并在一个样式文件中定义:
@颜色/原色
@style/FooBar.DialogTheme
添加:
@颜色/原色
包装内容
包装内容
解决了问题。第一个图像视图将填充屏幕,因为它具有匹配父对象是如果需要,图像将仅为其大小分配空间,然后使用换行内容,否则需要全屏显示图像以匹配父对象另一种情况是:维度在dimens.xml中为sw720dp定义,但是它没有为低于sw720dp的设备定义,您可以使用这个dimen变量来设置布局宽度,它将在小于sw720dp的设备上崩溃
<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>
<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>