XML代码:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
android:id="@+id/maintab_radio_tab1"
style="@style/tab_button"
android:checked="true"
android:text="主页"
android:background="#ff00ff00" />
android:id="@+id/maintab_radio_tab2"
style="@style/tab_button"
android:text="分页"
android:background="#ff00ff00" />
android:id="@+id/maintab_radio_tab3"
style="@style/tab_button"
android:text="设置"
android:background="#ff00ff00" />
style="@style/tab_button"文件:
fill_parent
wrap_content
marquee
@null
1dp
1.0
center_horizontal
true
1.0dip
布局结构:(将TabWidget隐藏,用一组去掉button勾选框的RadioButton替代)
java代码:public class MainActivity extends TabActivity implements OnClickListener
{
private RadioButton mBtn_tab1;
private RadioButton mBtn_tab2;
private RadioButton mBtn_tab3;
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initTab();
}
//初始化界面
private void init()
{
mBtn_tab1 = (RadioButton)findViewById(R.id.maintab_radio_tab1);
mBtn_tab2 = (RadioButton)findViewById(R.id.maintab_radio_tab2);
mBtn_tab3 = (RadioButton)findViewById(R.id.maintab_radio_tab3);
mBtn_tab1.setOnClickListener(this);
mBtn_tab2.setOnClickListener(this);
mBtn_tab3.setOnClickListener(this);
}
//初始化底部标签栏
private void initTab()
{
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1").setContent(new Intent(this, Tab1Activity.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("tab2").setContent(new Intent(this, Tab2Activity.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("tab3").setContent(new Intent(this, Tab1Activity.class)));
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.maintab_radio_tab1:
mTabHost.setCurrentTabByTag("tab1");
break;
case R.id.maintab_radio_tab2:
mTabHost.setCurrentTabByTag("tab2");
break;
case R.id.maintab_radio_tab3:
mTabHost.setCurrentTabByTag("tab3");
break;
default:
break;
}
}
}