TabHost之心得

MainActivity:

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);

		tabHost = getTabHost();
		LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab1",
				getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_Blue));
		tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab2",
				getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_green));
		tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab3",
				getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_red));

	}


 

主布局:

 
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/widget_layout_Blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
<!-- 静态加载布局 -->
        <include
           android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/tab1" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/widget_layout_red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/tab3" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/widget_layout_green"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/tab2" />
    </LinearLayout>

</FrameLayout>



 

tab布局文件分别为tab1,tab2,tab3 使用<include/>标签可以静态加载tab布局,更方便布局(主布局中的LinearLayout可以为相对布局)

tab1使用的是RelativeLayout布局

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值