底部导航栏高度复用

经常要用到底部导航栏,所以就写了个demo,使用单例模式,高度模块化的思想。

里面设计一个导航栏的管理类和导航栏单独的布局文件(使用include标签加载到其他页面即可)

管理类:管理一个radiogroup

package com.example.manage;

import java.util.RandomAccess;

import com.example.reuse_headbotton.R;

import android.app.Activity;
import android.content.Context;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class BottomManager {

	RadioGroup  rg;
	RadioButton news1, news2, news3, news4;

	/*
	 * 单例模式,保证只存在一个类来处理所有调用
	 */
	
	public static BottomManager bottomManager=new BottomManager();

	public static BottomManager getInstance() {
		return bottomManager;
	}

	/*
	 * 找到组件,设置监听,需要手动调用
	 */
	public void initsDate(Activity ac) {
		rg=(RadioGroup) ac.findViewById(R.id.id_radiogroup);
		rg.setOnCheckedChangeListener(new LRadioGroupListener());
	}

	/*
	 * 给radiogroup设置的监听器,L是我自己添加的区分
	 * 在适配的界面进行你们的调用,或者传递某组数据都可以
	 */
	class LRadioGroupListener implements RadioGroup.OnCheckedChangeListener {

		public void onCheckedChanged(RadioGroup group, int checkedId) {
			System.out.println("打印当前点击的按钮"+checkedId);
			switch (checkedId) {
			case R.id.id_news:
				//TODO:匹配上按钮1做的事
				break;
			case R.id.id_news2:
				//TODO:匹配上按钮2做的事
				break;
			case R.id.id_news3:
				//TODO::匹配上按钮3做的事
				break;
			case R.id.id_news4:
				//TODO::匹配上按钮4做的事
				break;

			default:
				break;
			}
		}

	}
}


导航栏的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/id_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/id_news"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="新闻1" />

        <RadioButton
            android:id="@+id/id_news2"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="新闻2" />

        <RadioButton
            android:id="@+id/id_news3"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="新闻3" />

        <RadioButton
            android:id="@+id/id_news4"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="新闻4" />
    </RadioGroup>

</LinearLayout>

本例源码可从http://pan.baidu.com/s/1qWPpDSC下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值