2010-04-13 29 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

相关问题