我有一个CustomView类,我想为其使用xml布局。因此,我的类扩展了RelativeLayout,扩大了xml布局,并尝试将其附加到self上。

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>

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

UPDATE :上面的代码正确。
正如secretlm所提到的,问题在于<merge>被用作root element并被夸大为另一段od代码:
ArrayAdapter<String> adapter =
    new ArrayAdapter<String>(this,
                             R.layout.layers_list_item,
                             R.id.layers_list_item_text);

并在添加每个元素的过程中,适配器尝试对以R.layout.layers_list_item为根的<merge>进行充气。

最佳答案

如果没有容器元素,则不能在最终布局中将<merge>用作root element。 “当您知道此布局将放置在已经包含适当父 View 以包含该元素子元素的布局中时,将其用作根元素非常有用。当您计划将此布局包含在另一个布局中时,这特别有用。文件使用,并且此布局不需要其他ViewGroup容器”-来自“developer.android.com”

这个例子说明了如何使用<merge>: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);
    }
}

主要 Activity :
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"
    />

09-10 06:08
查看更多