底部菜单栏(二) TabHost & RadioGroup 实现

需求:使用TabHost & RadioGroup实现底部菜单栏;

效果图:

实现分析:

1.目录结构:

代码实现:

1. 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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <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_radiogroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@drawable/tab_widget_background"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:padding="2dip" >

            <RadioButton
                android:id="@+id/RadioButton0"
                style="@style/tab_item_background"
                android:checked="true"
                android:drawableTop="@drawable/tab_icon1"
                android:text="主页" />

            <RadioButton
                android:id="@+id/RadioButton1"
                style="@style/tab_item_background"
                android:drawableTop="@drawable/tab_icon2"
                android:text="关于" />

            <RadioButton
                android:id="@+id/RadioButton2"
                style="@style/tab_item_background"
                android:drawableTop="@drawable/tab_icon3"
                android:text="设置" />

            <RadioButton
                android:id="@+id/RadioButton3"
                style="@style/tab_item_background"
                android:drawableTop="@drawable/tab_icon4"
                android:text="搜索" />

            <RadioButton
                android:id="@+id/RadioButton4"
                style="@style/tab_item_background"
                android:drawableTop="@drawable/tab_icon5"
                android:text="更多" />
        </RadioGroup>
    </LinearLayout>

</TabHost>

2. styles.xml

    <style name="item_tab_text_style">
        <item name="android:textSize">10.0dip</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:singleLine">true</item>
    </style>

    <style name="tab_item_background">
        <item name="android:textAppearance">@style/item_tab_text_style</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/tab_background_selector</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:drawablePadding">3.0dip</item>
        <item name="android:layout_weight">1.0</item>
    </style>

3. MainActivity.java

package com.jjc.demo;

import com.jjc.demo.Constant.ConValue;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

    //定义TabHost对象
    private TabHost mTabHost;  

    //定义RadioGroup对象
    private RadioGroup radioGroup; 

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

        initView();
        initData();
    }

    /**
     * 初始化组件
     */
    private void initView() {
        // 实例化TabHost对象,得到TabHost
        mTabHost = getTabHost();

        // 得到Activity的个数
        int count = ConValue.mTabClassArray.length;

        for (int i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabSpec tabSpec = mTabHost.newTabSpec(ConValue.mTextViewArray[i]).setIndicator(ConValue.mTextViewArray[i]).setContent(getTabItemIntent(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec);
        }

        //实例化RadioGroup
        radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
    }

    private void initData(){
        // 给radioGroup设置监听事件
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.RadioButton0:
                    mTabHost.setCurrentTabByTag(ConValue.mTextViewArray[0]);
                    break;
                case R.id.RadioButton1:
                    mTabHost.setCurrentTabByTag(ConValue.mTextViewArray[1]);
                    break;
                case R.id.RadioButton2:
                    mTabHost.setCurrentTabByTag(ConValue.mTextViewArray[2]);
                    break;
                case R.id.RadioButton3:
                    mTabHost.setCurrentTabByTag(ConValue.mTextViewArray[3]);
                    break;
                case R.id.RadioButton4:
                    mTabHost.setCurrentTabByTag(ConValue.mTextViewArray[4]);
                    break;
                default:
                    break;
                }

            }});
    }

    /**
     * 给Tab选项卡设置内容(每个内容是一个Activity)
     */
    private Intent getTabItemIntent(int index) {
        Intent intent = new Intent(this, ConValue.mTabClassArray[index]);
        return intent;
    }
}

总结:android:textAppearance设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。

代码:http://pan.baidu.com/s/1nh9X8

时间: 2024-08-01 04:17:31

底部菜单栏(二) TabHost & RadioGroup 实现的相关文章

Android底部菜单栏(用TabHost一次性加载耗内存)

上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率.今天把demo贴出来一是方便以后自己查询,二是希望同时也能帮到大家. 底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏做.我这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了很多不必要的层,个人看起来还是不错的,所以贴出来方便以后使用). 先看一下

底部菜单栏(一) TabHost实现

需求:使用TabHost实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1.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 UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法

原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃用,那么我就不再浪费口水继续讲解它了,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Activity十分的相似,这一篇我花大量的篇幅来详细的讲解Fragment的介绍和使用方法. 一.Fragment的基础知识介绍  

【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏

转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8989063       从这一篇文章开始,我们将进入到一个应用程序主界面UI的开发和设计中了,底部菜单栏在Android的应用开发当中占有非常重要的地位.几乎所有的手机应用程序都有底部菜单栏这样的控件,主要是因为手机的屏幕大小有限,这样一种底部菜单栏实现起来的效果可以很方便的为用户切换自己所需要的界面,具有更强的交互性.底部菜单栏的样式和效果也是五花八门,多的数不胜数,但是

安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)

关于底部菜单是什么,我想没必要介绍了,在市场上的APP里太常见了,这里提供两种方式来实现. 记得之前写过几篇关于底部菜单实现的方法,有兴趣的朋友可以看看: 1.<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)> 2.<安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航)> 3.<安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)> 今天带来种相对更通俗易懂的写法,不再和过去一样去沿用TabHost了

关于安卓开发实现底部菜单栏

将TabHost的标签放在底部 直接上代码 主代码: 1 package sdut; 2 3 import com.example.sdutfriends.R; 4 5 import android.app.AlertDialog; 6 import android.app.TabActivity; 7 import android.content.DialogInterface; 8 import android.content.Intent; 9 import android.os.Bund

【Android开发笔记】底部菜单栏 FragmentTabHost

公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方 结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵 查了查资料发现实现底部菜单栏用的是FragmentTabHost,下面记录下具体如何实现的 首先,假设我有3个菜单栏,对应3个Fragment:FragmentA.FragmentB.FragmentC 这3个Fragment将由3个一个Activity控制:TabHostActivity TabHostActivity对应的xml文件

TabActivity实现底部菜单栏(六)

滴水穿石不是靠力,而是因为不舍昼夜. 本讲内容:TabActivity实现底部菜单栏 TabActivity这个类已经在Android4.0的系统中被弃用了,新的应用程序应该使用Fragment来代替该类的开发 示例效果图      (一)第一种实现方式:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏.这种方式更漂亮,也更灵活,大部分的应用程序基本都是使用这种方式,通过setCurrentTabByTag()方法来切换不同的选项卡. 下面是res/layo

Android仿微信底部菜单栏+顶部菜单栏(附源码)

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文要实现仿微信微信底部菜单栏+顶部菜单栏,采用ViewPage来做,每一个page对应一个XML,当手指在ViewPage左右滑动时,就相应显示不同的page(其实就是xml)并且同时改变底部菜单按钮的图片变暗或变亮,同时如果点击底部菜单按钮,左右滑动page(其实就是xml)并且改变相应按钮的亮度. 最终效果:源码免费下载 一.布局 1.顶部菜单布局,命名为top_layout.xml