本文介绍了在充气Android的XML合并布局错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CustomView 类,我想用XML布局吧。所以,我的类扩展 RelativeLayout的,膨胀XML布局,并试图将其附加到自己。

I have a CustomView class and I want to use xml layout for it. So, my class extends RelativeLayout, inflates xml layout and tries to attach it to self.

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

如果我的XML的布局有一定的布局(线性为例)的根元素,它工作正常。但是,当我尝试使用<合并> 根据这种反应我得到这个错误:

If my xml layout has some layout (Linear, for example) as root element it works fine. But when I try to use <merge> tag according to this response I got this error:

&LT;合并/&GT;只能用于具有有效的ViewGroup根和attachToRoot =真

我的XML布局是这样的:

My xml layout is like:

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

     <CheckBox
        ... />

     <TextView
        ... />

</merge>

我也试着删除&LT所有属性;合并...&GT; 标签,得到了相同的结果。怎么了?

I also tried to remove all attributes from <merge... > tag, got the same result. What's wrong?

更新:在code以上是正确的。 由于 secretlm 的所提到的,问题是,&LT;合并&GT; 被用来作为根元素和膨胀的另一块OD code:

UPDATE: The code above is correct. As secretlm mentioned, problem was that <merge> was used as a root element and inflated in another piece od code:

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

和使用的每一个元素添加的适配器试图夸大 R.layout.layers_list_item 具有&LT;合并&GT; 作为根。

And with every element added adapter tried to inflate R.layout.layers_list_item which have <merge> as root.

推荐答案

您无法使用&LT;合并&GT; 根元素在你的最终布局没有容器元素。当你知道这种布局将被放置到已包含相应的父视图包含元素的儿童布局以此为根元素是非常有用的。当你计划包括在其他布局此布局这是特别有用文件中使用这种布局不要求有不同的ViewGroup集装箱 - 从developer.android.com

you can't use <merge> as a root element in your final layout without container element. "Using this as the root element is useful when you know that this layout will be placed into a layout that already contains the appropriate parent View to contain the children of the element. This is particularly useful when you plan to include this layout in another layout file using and this layout doesn't require a different ViewGroup container" -- from "developer.android.com"

这个例子表明,如何与&LT工作;合并&GT; :<一href="http://www.$c$crzheaven.com/2011/06/26/merge-two-layout-xml-in-android/">http://www.$c$crzheaven.com/2011/06/26/merge-two-layout-xml-in-android/

This example show that how to work with <merge>: http://www.coderzheaven.com/2011/06/26/merge-two-layout-xml-in-android/

更新:

您应当尝试构造从这个例子(背景下,的AttributeSet)。我认为这将解决您的问题。

You should try with Constructor(Context, AttributeSet) from this example. I think it will solve your problem.

文件的test.xml:

file 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的扩展测试类:

Test class which extend from 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"
    />

这篇关于在充气Android的XML合并布局错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:14