Layout can also include Don't you hear me?
Include is adding a well-written layout to another layout
Include benefits include:
- Reduce the amount of code in the main layout file and look clearer.
- Separate the parts to facilitate management.
- Can be reused in multiple places
And then we'll usually write a head and a foot.
In the layout of the Lord
It is best not to include directly
Better add a layer of linearlayout
And then set them up on the top or the bottom.
Such as
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout android:layout_width="Match_parent"android:layout_height ="Match_parent"xmlns:android="Http://schemas.android.com/apk/res/android"> <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:layout_alignparenttop="true"> <include layout="@layout/header"></include> </linearlayout> <linearlayoutandroid:id= "@+id/framemenu"android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:layout_alignparentbottom ="true"> <include layout="@layout/footer"></include> </linearlayout></relativelayout>
Android:layout_alignparenttop= "True" is on top of
Android:layout_alignparentbottom= "True" is on the bottom
The ID of the control after the include is also the set ID, as long as the corresponding view reference is good.
The head and feet are set.
And then there's the middle part.
We use Fragmlayout
Put him in the middle of the foot and the head.
Don't forget to add an ID to this fragmlayout.
Fragment load fast, lightweight
Main activity We use fragmentactivity
Create a new class that inherits from fragment after you have finished fragmentactivity
In this fragment.
Overriding the Oncreateview method
Parse layout, return to view
@Nullable @Override publiconCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //这里的layout换成自己想要的layout View view=inflater.inflate(R.layout.activity_scan,null); return view; }
Okay, back to the main activity.
It's easy to get the middle framlayout to load our fragmet.
FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();fragmentTransaction.replace(R.id.frame_content, new TestFragment());fragmentTransaction.commit();
Loading is done in just three steps
So the UI framework is done.
Android Diary--Build app UI framework with include and Framelayout