Android Fragment created

Source: Internet
Author: User

1. Using XML tags

1.1 Defining two important properties

<fragment
android:id= "@+id/fregment_top"
Android:layout_width= "Match_parent"
android:layout_height= "220DP"
android:layout_margintop= "80DP"
android:name= "Com.lzh.fragment_study. Framgment_first "
/>

1.2 Implementing a new class that inherits from fragment

public class Framgment_first extends Fragment {

Public Framgment_first () {
}

@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View Rootview = inflater.inflate (R.layout.fragment_main, container,
FALSE);
return rootview;
}
}

1.3 Create a fragment layout file Fragment_main

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= "Com.lzh.fragment_study. Mainactivity$placeholderfragment ">

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"
/>

</RelativeLayout>

2. Implementation via Java code

Adding fragment to the program
Fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Examplefragment fragment = new Examplefragment ();
Fragmenttransaction.add (R.id.container, fragment);
Fragmenttransaction.commit ();

Getsupportfragmentmanager (). BeginTransaction ()
. Add (R.id.container,new Framgment_first ()). commit ();

Android Fragment created

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.