Fragment are fragments that can be involved in the activity to understand the fragment, because they all can include the layout, all have their own life cycle.
Below we want to let the main activity include two fragments, and let two pieces full of screen
1. First, create a new two-fragment layout file
Left.xml
<?xml version="1.0" encoding="utf-8"?
><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =" match_parent " android:orientation =; <buttonandroid:id="@+id/button"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_gravity=" Center _horizontal "android:text=" button " /> </linearlayout>
Right.xml
<?xml version="1.0" encoding="utf-8"?
><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =" match_parent " android:background =" #00ff00 " android:orientation = "vertical" ; <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_gravity="Center_horizontal"android:textsize="20sp" Android:text="right"/> </linearlayout>
2. Next, create a new Leftfragment class and Rightfragment class, inherited from Fragment.
public class leftfragment extends fragment { @Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View view=inflater.inflate (r.layout.left, Container,false ); return view; }}
publicclass RightFragment extends Fragment { @Override publiconCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.right, container,false); return view; }}
3. Finally. Change the code in Activity_main.xml
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =; <fragment android:id = "@+id/left_fragment" android:name =< Span class= "Hljs-value" > "com.example.fragment.LeftFragment" android:layout_ Width = "0DP" android:layout_height =< Span class= "Hljs-value" > "match_parent" android:layout_weight = "1" /> <fragmentandroid:id= "@+id/right_fragment"android:name=" Com.example.fragment.RightFragment "android:layout_width=" 0DP "android:layout_height ="Match_parent"android:layout_weight="1" /> </linearlayout>
4. Effects such as those seen in the
5. Precautions:
At that time, the new Leftfragment class and the Rightfragment class did not change the return value to return view;
Android:name= "Com.example.fragment.LeftFragment" Can't write wrong
Android Notes (11) First Fragment