Realize thinking:
1. Need to write 2 or more sub-layouts
2. Write a Java class to implement the layout of the child and the parent class. (A sub-layout corresponds to a class)
3. Import the fragment layout in the parent class layout, and add the android:name="" attribute;
1. Write 2 layouts:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"> <TextView android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="The layout below" android:textSize="30sp" android:textColor="@color/colorBlack" android:maxEms="1" android:maxLines="5" android:layout_gravity="center" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="The layout above" android:textSize="30sp" android:textColor="@color/colorBlack" android:maxEms="1" android:maxLines="5"/> </LinearLayout>
2. Java class to achieve the sub-layout and parent-class layout
package com.example.lenovo.mydebrisdemo; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by lenovo on 2018/5/5. */ public class Top extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.top_fragment,container,false); return view; } }
package com.example.lenovo.mydebrisdemo; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by lenovo on 2018/5/5. */ public class Button extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.botton_fragment,container,false); return view; } }
3. Import the fragment layout in the parent class layout, and add the android:name="" attribute;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--id must be added or an error will be reported --> <fragment android:id="@+id/top" android:name="com.example.lenovo.mydebrisdemo.Top" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <fragment android:id="@+id/button" android:name="com.example.lenovo.mydebrisdemo.Button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
running result: