[控件]TabHost选项卡



import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class mainActivity extends TabActivity {
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabhost = getTabHost();
        tabhost.addTab(tabhost.newTabSpec("stats_tab").setIndicator("aaa")
                .setContent(R.id.no_team_meeting_stats));

        tabhost.addTab(tabhost.newTabSpec("meetings_tab").setIndicator("bbb")
                .setContent(R.id.no_team_meetings));
        getTabHost().setCurrentTab(0);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<TabWidget
			android:id="@android:id/tabs"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" />
		<FrameLayout
			android:id="@android:id/tabcontent"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent">
			<TextView
				android:id="@+id/no_team_meetings"
				android:textSize="18sp"
				android:layout_width="fill_parent"
				android:layout_height="fill_parent"
				android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
			<TextView
				android:id="@+id/no_team_meeting_stats"
				android:textSize="18sp"
				android:layout_width="fill_parent"
				android:layout_height="fill_parent"
				android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" />
		</FrameLayout>
	</LinearLayout>
</TabHost>
如果Activity继承至TabActivity则XML中的TabHost、TabWidget、FrameLayout的id分别固定为@android:id/tabhost、@android:id/tabs、@android:id/tabcontent
否则可自定义
在XML中使用LinearLayout可使内容不与选项卡标签重合
使用getTabHost();获得TabHost使用getTabWidget();获得TabWidget
添加选项卡:
使用TabHost的对象的addTab方法newTabSpec创建选项卡id
setIndicator设置选项卡文字和图片
选显卡可以选择 添加文字 、添加文字和背景图、使用自定义View
setContent设置内容
内容可以选择添加放在FrameLayout中的控件ID、Intent、TabContentFactory
使用TabContentFactory 
 .setContent(new TabContentFactory() {
    @Override
     public View createTabContent(String tag) {
      Log.e("a", tag);
      return null;
      }
}));
如果不是使用TabActivity的话TabHost的名字就可以自定义但TabWidget和FrameLayout的名字不能自定义
在使用findViewById获得TabHost必须使用
TabHost的对象的setup()方法,不然会报错
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值