ApiDemos之Tab

来源:互联网 发布:网络一线牵广场舞16步 编辑:程序博客网 时间:2024/05/18 01:54

1、通过findViewById

tabs1.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView android:id="@+id/view1"        android:background="@drawable/blue"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_1"/>    <TextView android:id="@+id/view2"        android:background="@drawable/red"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_2"/>    <TextView android:id="@+id/view3"        android:background="@drawable/green"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_3"/></FrameLayout>

java文件:

TabHost tabHost = getTabHost();                LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1")                .setContent(R.id.view1));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab2")                .setContent(R.id.view2));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(R.id.view3));
效果:

2、Content By Factory

public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("tab2")                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(this));    }    /** {@inheritDoc} */    public View createTabContent(String tag) {        final TextView tv = new TextView(this);        tv.setText("Content for tab with tag " + tag);        return tv;    }}

效果:

3、Content By Intent

List8.java

public class List8 extends Activity{}

public class Tabs3 extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("list")                .setContent(new Intent(this, List1.class)));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("photo list")                .setContent(new Intent(this, List8.class)));                // This tab sets the intent flag so that it is recreated each time        // the tab is clicked.        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("destroy")                .setContent(new Intent(this, Controls1.class)                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));    }}

效果:


4、增加holo主题

<activity android:name=".view.Tabs4" android:label="Views/Tabs/4. Non Holo theme"                  android:theme="@android:style/Theme">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.SAMPLE_CODE"/>            </intent-filter>        </activity>

效果:


5、可滑动Tab页

tabs_scroll.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:padding="5dp">        <HorizontalScrollView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:scrollbars="none">            <TabWidget                android:id="@android:id/tabs"                android:layout_width="match_parent"                android:layout_height="wrap_content" />        </HorizontalScrollView>        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:padding="5dp" />    </LinearLayout></TabHost>

Tabs5.java

public class Tabs5 extends TabActivity implements TabHost.TabContentFactory {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.tabs_scroll);        final TabHost tabHost = getTabHost();        for (int i=1; i <= 30; i++) {            String name = "Tab " + i;            tabHost.addTab(tabHost.newTabSpec(name)                    .setIndicator(name)                    .setContent(this));        }    }    /** {@inheritDoc} */    public View createTabContent(String tag) {        final TextView tv = new TextView(this);        tv.setText("Content for tab with tag " + tag);        return tv;    }}

效果:



0 0
原创粉丝点击