2010-04-13 31 views
81

在我的Android佈局文件中使用<包括>時,我無法覆蓋屬性。當我搜索的錯誤,我發現拒絕Issue 2863Android XML佈局的「包含」標籤真的有用嗎?

「包括標籤壞了(覆蓋佈局PARAMS從未工程)」

由於羅曼表示這部作品在測試套件和他的例子,我必須做的事情錯誤。

我的項目是組織這樣的:

res/layout 
    buttons.xml 

res/layout-land 
    receipt.xml 

res/layout-port 
    receipt.xml 

的buttons.xml包含這樣的事情:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button .../> 

    <Button .../> 
</LinearLayout> 

而且縱向和橫向receipt.xml文件看起來是這樣的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    ... 

    <!-- Overridden attributes never work. Nor do attributes like 
     the red background, which is specified here. --> 
    <include 
     android:id="@+id/buttons_override" 
     android:background="#ff0000" 
     android:layout_width="fill_parent" 
     layout="@layout/buttons"/> 

</LinearLayout> 

我錯過了什麼?

回答

129

我剛發現這個問題。首先,你只能覆蓋layout_ *屬性,所以背景不起作用。這是記錄的行爲,只是我的疏忽。

真正的問題是在LayoutInflater.java發現:

// We try to load the layout params set in the <include /> tag. If 
// they don't exist, we will rely on the layout params set in the 
// included XML file. 
// During a layoutparams generation, a runtime exception is thrown 
// if either layout_width or layout_height is missing. We catch 
// this exception and set localParams accordingly: true means we 
// successfully loaded layout params from the <include /> tag, 
// false means we need to rely on the included layout params. 
ViewGroup.LayoutParams params = null; 
try { 
    params = group.generateLayoutParams(attrs); 
} catch (RuntimeException e) { 
    params = group.generateLayoutParams(childAttrs); 
} finally { 
    if (params != null) { 
    view.setLayoutParams(params); 
    } 
} 

如果<包括>標籤不包括 layout_width和layout_height的RuntimeException的發生,並默默處理,沒有任何日誌語句甚至。

如果您要覆蓋任何layout_ *屬性,解決方案是始終在使用<包括>標記時同時包括layout_width和layout_height。

我的例子應該變爲:

<include 
     android:id="@+id/buttons_override" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/buttons"/> 
+33

這是荒謬的。我從來沒有能夠得到這個工作,我甚至看到提及需要高度和寬度的文檔,如果試圖覆蓋尺寸,我認爲它是高度和寬度。然而,我試圖覆蓋的是邊際,這不是一個維度。爲什麼當我想要改變的是layout_marginRight時,我需要指定這些還是其中的任何一個? Grrr,安卓系統,有時你讓我感到沮喪。 – 2010-12-14 02:06:17

+2

埃裏克,你應該接受你自己的答案作爲解決方案。 – 2010-12-14 02:06:41

+0

僅供參考如果您不覆蓋高度和寬度屬性 – binary 2015-01-16 21:46:05

2

我發現有時候我會想念包括Android:使用Eclipse中的GUI Builder的ID標籤。確保(當我注意到)我從構建器添加到TextView中時,我在ListView佈局中使用的ID。

<TextView android:text="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
... 

成爲

<TextView android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
... 

非但沒有 '假', '假',我得到:),幷包括工作確定。

10

我提交的enhancement request,以允許包含的所有屬性被重寫:

假設我具有比 TextView字段的值其他兩個相同的佈局。目前,我要麼在 運行時修改佈局,要麼複製XML。

例如與值 「你好」 和 「世界」 傳遞兩個參數來佈局1:

<include layout="@layout/layout1a" params="textView=hello|editText=world" />

layout1a.xml:

<merge><TextView text="@param/textView"><EditText hint="@param/editText"></merge>

另一種實現將打破封裝並允許 include語句覆蓋如下值:

<include layout="@layout/layout1b" overrides="@id/textView.text=hello|@id/editText.hint=world" />

layout1b.xml:

<merge><TextView id="@+id/textView"><EditText hint="@+id/editText"></merge>

+1

不知道爲什麼這有2倒數 – 2014-05-12 09:20:18

+0

考慮到新的數據綁定,僅供參考Android Lint會給您一個錯誤(佈局參數layout_height忽略,除非layout_width也在標籤上指定)東西,''現在更常使用,attr覆蓋是一個真正必須具備的功能 – 2016-02-25 16:02:49

相關問題