TabHost使用简介

TabActivity 类已经被放弃,所以只继承Activity就可以

main.xml

<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:background="@color/white" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
			<!-- TabWidget的id,建议不要修改 -->
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
			<!-- FrameLayout的id,建议不要修改 -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
				<!-- 可使用LinearLayout等布局设置其他控件 -->
				<!-- 标签1 -->
                <TextView
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/first" />
				<!-- 标签2 -->
                <TextView
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/second" />
				<!-- 标签3 -->
                <TextView
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/third" />
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>


string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TabHost</string>
    <string name="action_settings">Settings</string>
    <string name="first">我是第一选项卡啊啊啊</string>
    <string name="second">我是第二选项卡啊啊啊</string>
    <string name="third">我是第三选项卡啊啊啊</string>
</resources>

MainActivity.java

private TabHost th;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		th = (TabHost) findViewById(R.id.tabhost);
		th.setup();
		
		//取得TabWidget,以便修改标签内容
		TabWidget tabWidget = th.getTabWidget();
				
		//添加三个标签
		th.addTab(th.newTabSpec("tab_1")
				.setIndicator("选项1", getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		
		th.addTab(th.newTabSpec("tab_2")
				.setIndicator("选项2", getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		
		th.addTab(th.newTabSpec("tab_3")
				.setIndicator("选项3", getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		
		
		//可设置TabWidget背景及布局等
		// tabWidget.getChildAt(1).setBackgroundColor(getResources().getColor(R.color.red));
		// tabWidget.getChildAt(0).setLayoutParams(new
		// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
		// LayoutParams.MATCH_PARENT));
		
		//标签图标为ImageView 对象,android.R.id.icon 为第一个标签的图片android.R.id.icon1为第二个标签图片
		ImageView img1 = (ImageView) tabWidget.getChildAt(0).findViewById(android.R.id.icon);
		img1.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		//可设置图片属性及更换图片
		img1.setImageDrawable(getResources().getDrawable(R.drawable.lgn0006));
		
		//标签文字为TextView 对象,android.R.id.title 为第一个标签的文字android.R.id.title1为第二个标签文字
		//可更改颜色字体等
		TextView textview = (TextView) tabWidget.getChildAt(0).findViewById(android.R.id.title);
		textview.setTextSize(10);
		
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值