知识点:
tabhost标签页运行截图:
自定义tabhost:
代码实现:
1、导入所需要的图片。
2、main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabhost"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<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="0dp"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/line1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1)章泽天是我的女神"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2)刘诗诗是我的女神"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/line3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="3)刘亦菲是我的女神"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
3、MainActivity
package com.njupt.tabhost1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends Activity {
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = (TabHost) findViewById(R.id.tabhost);
tabhost.setup();
TabSpec tab1 = tabhost.newTabSpec("tab1");
//tabhost: tab1.setIndicator("章泽天1", getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("章泽天1_1"));//自定义tabhost
tab1.setContent(R.id.line1);
tabhost.addTab(tab1);
TabSpec tab2 = tabhost.newTabSpec("tab2");
// tab2.setIndicator("章泽天2", getResources().getDrawable(R.drawable.i2));
tab2.setIndicator(createView("章泽天2_2"));
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);
TabSpec tab3 = tabhost.newTabSpec("tab3");
// tab3.setIndicator("章泽天3", getResources().getDrawable(R.drawable.i7));
tab3.setIndicator(createView("章泽天3_3"));
tab3.setContent(R.id.line3);
tabhost.addTab(tab3);
}
private View createView(String text){
View view = View.inflate(this, R.layout.tab, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_title.setText(text);
return view;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
---------------------------------------到这里tabhost标签页就写完了,以下介绍自定义tabhost-----------------------------
其实自定义tabhost,是在传统tabhost的基础上加载自己的布局就行了。。。。。
1、bg.xml
用于给自定义布局加上背景,常常放在drawable这个包下。。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@drawable/bg_selected"/>
<item android:drawable="@drawable/bg_normal"/>
</selector>
2、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/tv_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="22sp"
android:textColor="@android:color/white"
/>
</LinearLayout>
3、MainActivity
这是我们在setIndicator()时选择那一个义View 作为参数的方法即可
即: tab1.setIndicator(createView("章泽天1_1"));//自定义tabhost