您的位置:首页 > 其它

TabHost使用简介

2014-11-06 15:10 162 查看
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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: