Android 新浪微博底部TabHost

新浪微博之总结 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;
                }
            }
        });
    }
}

效果图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值