正常在使用tabhost的时候都会发觉默认布局在下方显示。在需求遇到在下方显示的时候则需要自己去重写方法;
完成效果图:
实现步骤:
1 设置按键效果的背景显示:
home_btn_bg.xml文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_btn_bg_n" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/home_btn_bg_s" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="@drawable/home_btn_bg_d" android:state_enabled="true" android:state_checked="true" />
</selector>
其中主要设置点击效果对应的显示背景
2 设置styles.xml文件代码如下:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="main_tab_bottom">
<item name="android:textSize">10.0dip</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">5.0dip</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">2.0dip</item>
<item name="android:layout_weight">1.0</item>
</style>
</resources>
这种布局主要用于显示模拟的tabhost的具体每一个tabhost选项卡的显示效果,style名为mian_tab_bottom
3 设置布局文件activity_main.xml文件代码如下:
<?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"
android:background="#FFFFFF">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone"/>
<RadioGroup
android:id="@+id/main_tab"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#87CEEB"
android:gravity="center_vertical"
android:layout_gravity="bottom">
<RadioButton
android:layout_marginTop="0.5dip"
android:text="医疗集团"
android:textColor="#000000"
android:drawableTop="@drawable/ic_launcher"
android:id="@+id/radio_button0"
style="@style/main_tab_bottom"/>
<RadioButton
android:layout_marginTop="0.5dip"
android:text="康复集团"
android:drawableTop="@drawable/ic_launcher"
android:textColor="#000000"
android:id="@+id/radio_button1"
style="@style/main_tab_bottom"/>
</RadioGroup>
</LinearLayout>
</TabHost>
每个tabhost选项卡具体可在这里显示:包括文字和图片;
4 java文件MainActivity.java代码如下:
package com.example.android;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Window;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.RadioButton;
public class MainActivity extends TabActivity implements
OnCheckedChangeListener {
private TabHost mHost;
private Intent first;
private Intent second;
RadioButton firstTabHost;
RadioButton secondTabHose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// ~~~~~~~~~~~~ 初始化
this.first = new Intent(this, First.class);
this.second = new Intent(this, Second.class);
initRadios();
setupIntent();
}
/**
* 初始化底部按钮
*/
private void initRadios() {
((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this);
}
private void setupIntent() {
this.mHost = getTabHost();
TabHost localTabHost = this.mHost;
localTabHost.addTab(
buildTabSpec("first_tab", "kangfu",
R.drawable.jianbi5, this.first));
localTabHost.addTab(buildTabSpec("second_tab", "nihao",
R.drawable.jianbi5, this.second));
}
/**
* 切换模块
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
switch (buttonView.getId()) {
case R.id.radio_button0:
this.mHost.setCurrentTabByTag("first_tab");
break;
case R.id.radio_button1:
this.mHost.setCurrentTabByTag("second_tab");
break;
}
}
}
private TabHost.TabSpec buildTabSpec(String tag, String resLabel, int resIcon,
final Intent content) {
return this.mHost
.newTabSpec(tag)
.setIndicator(resLabel,getResources().getDrawable(resIcon))
.setContent(content);
}
}
资源下载 http://download.youkuaiyun.com/detail/fengerfhf/5834491