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

Android 实现底部导航栏

2014-09-14 17:37 411 查看
Android 中的底部菜单栏,极大限度的弥补了屏幕大为我们带来的不足,现在我们可以看到各大应用都在采取这个以措施,开始的微信并没有采用该种方式但现在改版之后采用了底部导航栏,现在各大厂商出的旗舰机中大多为5.0以上的屏幕,最少也是在4.5之上,单手可操持的黄金尺寸是3.5,显然,当前手机屏幕完全违背了这一黄金原则,但是手机的大屏趋势是无法改变的,为了改变这些给人带来不方便的地方,所以我们通过各种手势和导航栏成了必不可少的。

首先要在layout中将布局写好

<?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:background="#ffffff"  
        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" />  
  
        <FrameLayout  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" >  
  
           <RadioGroup  
                android:id="@+id/main_tab_group"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_gravity="bottom"  
                android:background="@drawable/bottombg"  
                android:gravity="bottom"  
                android:orientation="horizontal"  
                 >

               <RadioButton
                   android:id="@+id/main_tab_eatting"
                   style="@style/MMTabButton"
                   android:button="@null"
                   android:layout_gravity="center_vertical|left"
                   android:layout_weight="1.0"
                   android:drawableTop="@drawable/a_eatting_check"
                   android:text="@string/eatting" />
  
                <RadioButton  
                    android:id="@+id/main_tab_feeling"  
                    android:layout_weight="1.0"  
                    android:button="@null"
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:drawableTop="@drawable/a_feeling_check"
                    style="@style/MMTabButton"
                    android:text="@string/feeling" />  
                <RadioButton  
                    android:id="@+id/main_tab_love_pole" 
                    android:button="@null"
                    android:checked="true" 
                    android:layout_weight="1.0"     
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:drawableTop="@drawable/a_love_pole_check"
                    style="@style/MMTabButton"
                    android:text="@string/love_pole" />  
                <RadioButton  
                    android:id="@+id/main_tab_mail_box"
                    android:button="@null"  
                    android:layout_weight="1.0"   
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:drawableTop="@drawable/a_mail_box_check"
                    style="@style/MMTabButton"
                    android:text="@string/mail_box" /> 
                <RadioButton  
                    android:id="@+id/main_tab_shopping" 
                    android:button="@null" 
                    android:layout_weight="1.0"    
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:drawableTop="@drawable/a_shopping_check"
                    style="@style/MMTabButton"
                    android:text="@string/shopping" /> 
                     
            </RadioGroup>  
  
            <ImageView  
                android:id="@+id/main_tab_new_message"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center_horizontal|top"  
                android:layout_marginLeft="60dip"  
                android:layout_marginTop="1dip"   
                android:gravity="center"  
                android:background="@drawable/unread_dot"
                />  
        </FrameLayout>  
    </LinearLayout>  
  
</TabHost>
上述的布局是将底部的布局写好,就是通过radioButton来实现的,此处的标题也是通过自定义的。

public class MainActivity extends TabActivity {
    private TabHost tabhost;
    private TextView title_text;
    private static boolean login=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
	setContentView(R.layout.tabhost);
	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_icon);
	title_text = (TextView) findViewById(R.id.title_text);
	tabhost = MainActivity.this.getTabHost();
	TabHost.TabSpec spec;
	Intent intent;
	
	intent = new Intent ().setClass(this, Eatting.class);
	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	spec = tabhost.newTabSpec("吃饭").setIndicator("吃饭").setContent(intent);  
        tabhost.addTab(spec);
        
        
        intent = new Intent ().setClass(this, Feeling.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabhost.newTabSpec("心情").setIndicator("心情").setContent(intent); 
        tabhost.addTab(spec);
        
        intent = new Intent ().setClass(this, MailBox.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabhost.newTabSpec("信箱").setIndicator("信箱").setContent(intent); 
        tabhost.addTab(spec);
        
        intent = new Intent ().setClass(this, Shopping.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabhost.newTabSpec("购物").setIndicator("购物").setContent(intent); 
        tabhost.addTab(spec);
        
        intent = new Intent ().setClass(this, Love_Pole.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabhost.newTabSpec("爱爱轴").setIndicator("爱爱轴").setContent(intent); 
        tabhost.addTab(spec);
        
        tabhost.setCurrentTabByTag("爱爱轴");
        title_text.setText("爱爱时间轴");
        RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);  
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {         
            @Override  
            public void onCheckedChanged(RadioGroup group, int checkedId) {  
                // TODO Auto-generated method stub  
                switch (checkedId) {  
                case R.id.main_tab_eatting:  
                    tabhost.setCurrentTabByTag("吃饭");
                    title_text.setText("一起吃饭");
                    break;  
                case R.id.main_tab_feeling:  
                    tabhost.setCurrentTabByTag("心情");
                    title_text.setText("晒心情");
                    break;  
                case R.id.main_tab_love_pole:  
                    tabhost.setCurrentTabByTag("爱爱轴");
                    title_text.setText("爱爱时间轴");
                    break;  
                case R.id.main_tab_shopping:
                    tabhost.setCurrentTabByTag("购物");
                    title_text.setText("Let's 购");
                    break;  
                case R.id.main_tab_mail_box:  
                    tabhost.setCurrentTabByTag("信箱");
                    title_text.setText("信箱");
                    break;  
                default:  
                    //tabHost.setCurrentTabByTag("我的考试");  
                    break;  
                }  
            }  
    });
    }
这样便可以实现一个简单的底部导航栏的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: