Android Notes (11) First Fragment

Source: Internet
Author: User

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

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.