24

クラスがあり、 CustomViewxmlレイアウトを使用したいと思います。したがって、私のクラスは拡張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レイアウトにルート要素としていくつかのレイアウト(たとえば、線形)がある場合、それは正常に機能します。しかし、この応答<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... >また、タグからすべての属性を削除しようとしましたが、同じ結果が得られました。どうしたの?

更新:上記のコードは正しいです。secretlmが述べたように、問題は、 aとして<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>

4

2 に答える 2

35

コンテナ要素がないと、最終的なレイアウトで<merge>として使用することはできません。root element「これをルート要素として使用すると、このレイアウトが、要素の子を含む適切な親ビューを既に含むレイアウトに配置されることがわかっている場合に役立ちます。これは、このレイアウトを別のレイアウトに含める場合に特に役立ちます。ファイルを使用し、このレイアウトには別のViewGroupコンテナは必要ありません」-「developer.android.com」から

この例は、次の操作方法を示しています:http <merge>: //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"      
    />
于 2012-12-26T11:47:01.817 に答える