Android Shard use

Source: Internet
Author: User

First create a new fragment layout file, <?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:gravity= "Center" >

<button
Android:id= "@+id/btnleftfrag"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "button in left fragment"/>

</LinearLayout>
Then, in the new class that controls the fragment, the class inherits from fragment. Package com.example.flypie.notesbook.Fragment;

Import android.app.Fragment;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;

Import COM.EXAMPLE.FLYPIE.NOTESBOOK.R;

/**
* Created by Flypie on 2015/12/11.
*/
public class Leftfragment extends fragment{

@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view=inflater.inflate (R.layout.left_fragment,container,false);
return view;
}
}
Add <fragment to the layout where you want to use the fragment
Android:id= "@+id/fragleft"
Android:name= "Com.example.flypie.notesbook.Fragment.LeftFragment"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1" tools:layout= "@layout/left_fragment"/>Note: The android:name specified class is executed when the layout is loadedfragment communication with your activity:In activity, different fragment:fragmentmanager Fragmentmanager = Getfragmentmanager () can be changed in the following ways; Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction (); Fragmenttransaction.replace ( r.id.rightlayout,anotherrightfragment); Fragmenttransaction.addtobackstack (null); Fragmenttransaction.commit ();Note: One of theR.id.rightlayout is the container that contains the fragment that is currently being replaced in fragment, you can get an instance of your activity in the following ways, and then call its method: Testfragmentactivity Testfragmentactivity = (testfragmentactivity) getactivity (); You can call an instance of the specified fragment by invoking the following method of activity to invoke its method: rightfragment rightfragment = (rightfragment) Testfragmentactivity.getfragmentmanager (). Findfragmentbyid (R.id.fragright);

Android Shard use

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.