2013-10-29 47 views
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> 

无论何时执行该代码:二进制XML文件行#7:您必须提供layout_width属性

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" 
+2

你会发现这个错误的另一种情况是:尺寸在dimens.xml为sw720dp定义,但它不是对设备的定义比sw720dp低,你用这个扪VAR设置layout_width,它会坠毁在更小的设备比sw720dp –

回答

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

2

添加android:layout_widthandroid:layout_heightImageView

这样

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

第一图像视图将充满整个屏幕,因为它已经match_parent – Raghunandan

+0

是,如果你想图像将空间分配给只有它的大小,然后使用完整的S WRAP_CONTENT否则想图像creen将其交给match_parent – morroko

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属性]

相关问题