Android xml merge layout error on inflate

前端 未结 2 1340
Happy的楠姐
Happy的楠姐 2020-12-30 23:59

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 s

相关标签:
2条回答
  • 2020-12-31 00:43
    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"      
        />`
    
    0 讨论(0)
  • 2020-12-31 00:48

    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"

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

    Updated:

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

    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>
    

    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);    
        }
    }
    

    Main activity:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }    
    }
    

    Main layout:

    <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"      
        />
    
    0 讨论(0)
提交回复
热议问题