Android Diary--Build app UI framework with include and Framelayout

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.