膨胀时Android xml合并布局错误

Fre*_*ase 18 android android-layout

我有一个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);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我的xml布局有一些布局(例如,线性)作为根元素,它可以正常工作.但是当我尝试<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>
Run Code Online (Sandbox Code Playgroud)

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

更新:上面的代码是正确的.正如秘密所提到的那样,问题是在另一件od代码中<merge>被用作root element和膨胀:

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(this, 
                             R.layout.layers_list_item, 
                             R.id.layers_list_item_text);
Run Code Online (Sandbox Code Playgroud)

随着时间一天天加入的元素适配器试图夸大R.layout.layers_list_item其具有<merge>作为根.

sec*_*tlm 27

你不能用<merge>作为一个root element在你的最终布局无容器元素."当您知道此布局将放置在已包含相应父视图的布局中以包含元素的子元素时,将此作为根元素非常有用.当您计划将此布局包含在另一个布局中时,这尤其有用文件使用和此布局不需要不同的ViewGroup容器" - 来自"developer.android.com"

此示例显示如何使用<merge>:http://www.coderzheaven.com/2011/06/26/merge-two-layout-xml-in-android/

更新:

您应该尝试使用此示例中的构造函数(Context,AttributeSet).我认为它会解决你的问题.

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>
Run Code Online (Sandbox Code Playgroud)

从RelativeLayout扩展的测试类:

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

主要活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }    
}
Run Code Online (Sandbox Code Playgroud)

主要布局:

<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"      
    />
Run Code Online (Sandbox Code Playgroud)

  • @Frederic:我更新了我的答案,希望它能解决你的问题. (2认同)