TabHost

MainActivity

public class MainActivity extends Activity {
  private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost=(TabHost) findViewById(R.id.tabhost);
tabhost.setup();//找到tabwidge和framLayout


//添加标签页
TabSpec tab1= tabhost.newTabSpec("tab1");
//指定标签
//tab1.setIndicator("首页",getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("首页"));
//指定标签页的内容
tab1.setContent(R.id.line1);
tabhost.addTab(tab1) ;


//添加标签页
TabSpec tab2= tabhost.newTabSpec("tab2");
//指定标签
//tab2.setIndicator("第二页",getResources().getDrawable(R.drawable.i2));
//指定标签页的内容
tab2.setIndicator(createView("第二页"));
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);
//添加标签页



TabSpec tab3= tabhost.newTabSpec("tab3");
//指定标签
//tab3.setIndicator("第三页",getResources().getDrawable(R.drawable.i3));
//指定标签页的内容
tab3.setIndicator(createView("第三页"));
tab3.setContent(R.id.line3);
tabhost.addTab(tab3);


}
  private View createView(String text){
 View view =View.inflate(this, R.layout.tap, null);
 TextView textview=(TextView) view.findViewById(R.id.textview);
textview.setText(text);
return view ;
 
  }

}

main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >


            <!-- 首页 -->


            <LinearLayout
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="首页"
                    android:textSize="30sp" />
            </LinearLayout>
            <!-- 第二页 -->


            <LinearLayout
                android:id="@+id/line2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="第二页"
                    android:textSize="30sp" />
            </LinearLayout>
            <!-- 第三页 -->


            <LinearLayout
                android:id="@+id/line3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="第三页"
                    android:textSize="30sp" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>


</TabHost>

TAB.xml

<?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:orientation="vertical"
    android:background="@drawable/bg"
     >
    <TextView 
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="22sp"
        />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值