FragmentTabHost用法

来源:互联网 发布:什么是嵌入式软件开发 编辑:程序博客网 时间:2024/05/21 13:36

xml

<LinearLayout 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:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:layout_weight="1"        android:id="@+id/content"        android:layout_width="wrap_content"        android:layout_height="0dp" >    </FrameLayout>    <android.support.v4.app.FragmentTabHost        android:id="@+id/tab"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

MainActivity

public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);FragmentTabHost tabHost = (FragmentTabHost) findViewById(R.id.tab);tabHost.setup(this, getSupportFragmentManager(), R.id.content);tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), MyFragment.class, null);Bundle b = new Bundle();b.putString("key", "value");tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher)), MyFragment.class, b);}}

MyFragment

package com.example.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.ListFragment;import android.widget.ArrayAdapter;public class MyFragment extends ListFragment{String show1[] = {"1","2","3","4"};String show2[] = {"4","3","2","1"};@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);String show[] = null;Bundle bundle = getArguments();if(bundle == null)show = show1;else {show = show2;System.out.println(bundle.get("key"));}setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, show));}}


原创粉丝点击