开发者

android how to use following code for Fragments

I am trying to do an application for Fragments. I am doing an example code present at,

http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html

this example has 2 xml files. I unable get where should i use that xml files. And which class should be my main ac开发者_StackOverflowtivity class. Plz anybody help me.

Thank you


The code for the main activity is not given in the blog. As author says "The code for this activity is not interesting; it just calls setContentView() with the given layout:".
So you have to create a dummy activity which just calls setContentView() with the Layout given in the first xml file.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

where main.xml whould contain:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent" />

</LinearLayout>

TitleFragment and DetailsFragment would go into seperate java files. The other xml and DetailsActivityis for handling Portrait mode in which case, the DetailsFragment is changed to a separate activity instead of a Fragment.

For more details refer the Example section of http://developer.android.com/guide/topics/fundamentals/fragments.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜