2013-10-29 49 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屬性]

相關問題