简体   繁体   中英

Custom view does not inflate from XML when referenced in Fragment.xml Android Application Development

I have created a custom view in Android lets call it MyCustomView and its XML file is MyCustomView.xml. I referance MyCustomView in a fragment lets call it MyFragment and MyFragment.xml. The reference is only an XML reference not code. When my fragment inflates, it inflates utilizing the MyFragment.xml. I can then retrieve a pointer to the view. Here is my problem. Everything works as expected except MyCustomView did not inflate from MyCustomView.xml thus, none of the buttons or layouts defined in MyCustomView.xml are seen on screen. What might I be doing wrong? How can I make my XML reference in MyFragment.xml inflate with MyCustomView.xml?

MyFragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.domain.MyFragment"
    android:id="@+id/MyFragment">

    <com.domain.MyCustomView
        android:id="@+id/MyCustomView"
        android:background="@android:color/transparent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    </com.domain.MyCustomView>

</FrameLayout>

MyCustomView.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="Cancel"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="Next"
            android:layout_weight="1" />

    </LinearLayout>

</FrameLayout>

MyFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_signature_capture, container, false);

        myCustomView = (MyCustomView) v.findViewById(R.id.signatureView);

        //At this point I would like to be able to 
        //access my buttons declared in MyCustomView.xml 
        //via the following call
       Button b1 = (Button) myCustomView.findViewById(R.id.btnCancel);

       //The above line returns null because it cant find the view. 



        return v ;
    }

You can only use a custom view in xml if you have defined a source (java) file for it which is a child of the View class or any of its descendants. Since you have only defined a custom layout, with no constructor, it cannot be inflated like a View can be. You should change:

<com.domain.MyCustomView
    android:id="@+id/MyCustomView"
    android:background="@android:color/transparent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
</com.domain.MyCustomView>

with:

<include
    layout=@layout/MyCustomView
    android:id="@+id/MyCustomView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</include>

and then move this attribute to your MyCustomView.xml root FrameLayout

android:background="@android:color/transparent"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM