您的位置:首页 > 移动开发 > Android开发

Android 新浪微博底部TabHost

2015-06-01 15:54 435 查看
新浪微博之总结 TabHost:TabHost能存放多个标签,并实现Activity的切换

注:TabHost在API 13停止使用,过几天我会写一个仿微信左右滑动切换的Demo

activity_main.xml:TabHost、TabWidget、FrameLayout的id是不能改变的。四个单选按钮放在一个Group中,并实现自定义的样式

<?xml version="1.0" encoding="utf-8"?>
<!-- TabHost组件id值为默认的 -->
<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:orientation="horizontal" >

<!-- TabWidget组件id值默认 -->

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
</TabWidget>
<!-- FrameLayout布局,id值默认 -->

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>

<RadioGroup
android:id="@+id/rg_main_btns"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bar"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/rd_home"
style="@style/main_btn_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/home_icon"
android:text="主页" />

<RadioButton
android:id="@+id/rd_at"
style="@style/main_btn_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/at_icon"
android:text="提到我" />

<RadioButton
android:id="@+id/rd_msg"
style="@style/main_btn_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/msg_icon"
android:text="信息" />

<RadioButton
android:id="@+id/rd_more"
style="@style/main_btn_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/more_icon"
android:text="查看更多" />
</RadioGroup>

</TabHost>


style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- 自定义按钮的风格 -->
<style name="main_btn_style">
<item name="android:button">@null</item>
<item name="android:textSize">10dp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:drawablePadding">4dp</item>
<item name="android:layout_weight">1.0</item>
<item name="android:background">@drawable/main_btn_bg_d</item>
</style>

</resources>


style中background的设置:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- drwable按下之后选择的图片,state_enabled:是否接受点击事件 state_pressed:是否被按下 -->
<item android:drawable="@drawable/main_btn_bg" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/main_btn_bg" android:state_checked="true" android:state_enabled="true"/>
</selector>


MainActivity.java:

/**
* @author Sony 继承TabActivity
*/
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

// 四个tab
private static final String HOME_TAB = "home";
private static final String AT_TAB = "at";
private static final String MSG_TAB = "msg";
private static final String MORE_TAB = "more";

private TabHost tabHost;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 获取TabHost
tabHost = this.getTabHost();
// 获取一个新的TabHost.TabSpec并关联到当前TabHost,设置指示器,设置跳转界面
TabSpec home = tabHost.newTabSpec(HOME_TAB).setIndicator(HOME_TAB)
.setContent(new Intent(this, HomeActivity.class));
TabSpec at = tabHost.newTabSpec(AT_TAB).setIndicator(AT_TAB)
.setContent(new Intent(this, AtActivity.class));
TabSpec message = tabHost.newTabSpec(MSG_TAB).setIndicator(MSG_TAB)
.setContent(new Intent(this, MessageActicty.class));
TabSpec more = tabHost.newTabSpec(MORE_TAB).setIndicator(MORE_TAB)
.setContent(new Intent(this, MoreActicty.class));

// 将页面填到tabHost中
tabHost.addTab(home);
tabHost.addTab(at);
tabHost.addTab(message);
tabHost.addTab(more);

// tabHost的选择事件
RadioGroup rGroup = (RadioGroup) this.findViewById(R.id.rg_main_btns);
// 监听事件
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int checkedId) {
// 根据选择设置当前tab
switch (checkedId) {
case R.id.rd_home:
tabHost.setCurrentTabByTag(HOME_TAB);
break;
case R.id.rd_at:
tabHost.setCurrentTabByTag(AT_TAB);
break;
case R.id.rd_msg:
tabHost.setCurrentTabByTag(MSG_TAB);
break;
case R.id.rd_more:
tabHost.setCurrentTabByTag(MORE_TAB);
break;
default:
break;
}
}
});
}
}


效果图:

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