【问题标题】:Android xml merge layout error on inflate充气时Android xml合并布局错误
【发布时间】:2012-12-26 10:20:13
【问题描述】:

我有一个CustomView 类,我想为它使用xml 布局。因此,我的课程扩展了RelativeLayout,扩展了 xml 布局并尝试将其附加到自身。

public class CustomView extends RelativeLayout
{
  public CustomView (Context context)
  {
     super(context);
     LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true);
  }
}

如果我的 xml 布局有一些布局(例如线性)作为根元素,它可以正常工作。但是当我尝试根据this response 使用<merge> 标签时,我得到了这个错误:

<merge /> can be used only with a valid ViewGroup root and attachToRoot=true

我的xml布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
     ... >

     <CheckBox
        ... />

     <TextView
        ... />

</merge>

我还尝试从&lt;merge... &gt; 标记中删除所有属性,得到了相同的结果。怎么了?

UPDATE:上面的代码是正确的。 正如secretlm 提到的,问题在于&lt;merge&gt; 被用作root element 并在另一段代码中膨胀:

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(this, 
                             R.layout.layers_list_item, 
                             R.id.layers_list_item_text);

随着添加的每个元素,适配器都试图膨胀R.layout.layers_list_item,它以&lt;merge&gt; 为根。

【问题讨论】:

  • 你能在 标签中更新你的孩子的观点吗?

标签: android android-layout


【解决方案1】:

您不能在没有容器元素的最终布局中使用&lt;merge&gt; 作为root element。 “当您知道此布局将被放置到已经包含适当父视图以包含该元素的子级的布局中时,将其用作根元素很有用。当您计划将此布局包含在另一个布局中时,这特别有用文件使用并且此布局不需要不同的 ViewGroup 容器”——来自“developer.android.com”

这个例子展示了如何使用&lt;merge&gt;:http://www.coderzheaven.com/2011/06/26/merge-two-layout-xml-in-android/

更新:

您应该尝试使用此示例中的 Constructor(Context, AttributeSet)。我想它会解决你的问题。

文件 test.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge
 xmlns:android="http://schemas.android.com/apk/res/android">

     <CheckBox
        android:id="@+id/layers_list_item_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/layers_list_item_root"    
        android:layout_alignParentRight="true"
        android:layout_marginLeft="15dp"        
        android:button="@drawable/ic_launcher" />

     <TextView
        android:id="@+id/layers_list_item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_toLeftOf="@id/layers_list_item_switch"
        android:selectAllOnFocus="true"
        android:text="tret"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:clickable="true" />

</merge>

从RelativeLayout扩展的测试类:

public class Test extends RelativeLayout
{       
    public Test(Context context, AttributeSet attrs) {
        super(context, attrs);      
        LayoutInflater.from(context).inflate(R.layout.test, this, true);    
    }
}

主要活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }    
}

主布局:

<com.example.testlayout.Test
    xmlns:android="http://schemas.android.com/apk/res/android"           
    android:id="@+id/layers_list_item_root"
    android:layout_height = "fill_parent"
    android:layout_width = "fill_parent"      
    />

【讨论】:

  • &lt;merge&gt; 不是root element - 它附加到CustomView,即RelativeLayout。无论如何,还有另一个更高级别的布局 - CustomView 只是描述 ListView 项目。
  • @Frederic:我更新了我的答案,希望它能解决你的问题。
  • 好吧,你对 root element 的看法是对的——root&lt;merge&gt; 作为 root 的 xml 用于 ArrayAdapter 构造函数,并在其中膨胀导致错误
  • 非常感谢这个解决方案,你让我很开心
  • ^ 和我的,从来不理解 inflate 末尾的布尔标志,现在我明白了
【解决方案2】:
Make sure to specify the complete path from source root in the main layout like this

`<com.example.testlayout.Test      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layers_list_item_root"
    android:layout_height = "fill_parent"
    android:layout_width = "fill_parent"      
    />`

and not like this 
`<Test      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layers_list_item_root"
    android:layout_height = "fill_parent"
    android:layout_width = "fill_parent"      
    />`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多