<?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"
最佳答案
将这些属性添加到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