最简单的android底部导航

本文介绍了一种使用自定义控件和Fragment实现Android底部导航的方法。通过创建BottomNavigation自定义控件,结合Fragment进行页面切换,实现了流畅的用户体验。文章详细展示了自定义控件的代码实现。
android底部导航(Bottom Navigation)实现--------------------------------------只需放到Xml里面就可以使用





1,利用技术点: 自定义控件 + Fragment 实现。








2,实现原理: 


~1,利用Fragment特性进行流畅切换。
~2,自定义控件:
#,因为每一个底部导航选项都有一个ImageView和TextView,而每次选中都会更改ImageView和TextView的图片或颜色,所以直接抽取成自定义控件方便循环利用。








3,自定义控件代码:






public class BottomNavigation extends RelativeLayout implements OnClickListener {


private RelativeLayout rlF, rlS, rlT, rlFo;
private ImageView ivF, ivS, ivT, ivFo;
private TextView tvF, tvS, tvT, tvFo;


public BottomNavigation(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.bottom_navigation, this);
findView();
set();


changeStateClick(ivF, tvF);


changeStateNormal(ivS, tvS);
changeStateNormal(ivT, tvT);
changeStateNormal(ivFo, tvFo);


}


private void set() {
rlF.setOnClickListener(this);
rlS.setOnClickListener(this);
rlT.setOnClickListener(this);
rlFo.setOnClickListener(this);


}


private void findView() {
rlF = (RelativeLayout) findViewById(R.id.rl_first);
ivF = (ImageView) findViewById(R.id.iv_first);
tvF = (TextView) findViewById(R.id.tv_first);
tvF.setText(R.string.home_first);


rlS = (RelativeLayout) findViewById(R.id.rl_second);
ivS = (ImageView) findViewById(R.id.iv_second);
tvS = (TextView) findViewById(R.id.tv_second);
tvS.setText(R.string.home_second);


rlT = (RelativeLayout) findViewById(R.id.rl_third);
ivT = (ImageView) findViewById(R.id.iv_third);
tvT = (TextView) findViewById(R.id.tv_third);
tvT.setText(R.string.home_third);


rlFo = (RelativeLayout) findViewById(R.id.rl_forth);
ivFo = (ImageView) findViewById(R.id.iv_forth);
tvFo = (TextView) findViewById(R.id.tv_forth);
tvFo.setText(R.string.home_forth);


}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_first:


HomePageFragment hf = new HomePageFragment();


((Activity) getContext()).getFragmentManager().beginTransaction()
.replace(R.id.fl, hf).addToBackStack(null).commit();


changeStateClick(ivF, tvF);


changeStateNormal(ivS, tvS);
changeStateNormal(ivT, tvT);
changeStateNormal(ivFo, tvFo);


break;
case R.id.rl_second:
ApplicationFragment apf = new ApplicationFragment();
((Activity) getContext()).getFragmentManager().beginTransaction()
.replace(R.id.fl, apf).addToBackStack(null).commit();


changeStateClick(ivS, tvS);
changeStateNormal(ivF, tvF);
changeStateNormal(ivT, tvT);
changeStateNormal(ivFo, tvFo);
break;
case R.id.rl_third:
ActionFragment af = new ActionFragment();
((Activity) getContext()).getFragmentManager().beginTransaction()
.replace(R.id.fl, af).addToBackStack(null).commit();


changeStateClick(ivT, tvT);
changeStateNormal(ivF, tvF);
changeStateNormal(ivS, tvS);
changeStateNormal(ivFo, tvFo);


break;
case R.id.rl_forth:


MineFragment mf = new MineFragment();
((Activity) getContext()).getFragmentManager().beginTransaction()
.replace(R.id.fl, mf).addToBackStack(null).commit();


changeStateClick(ivFo, tvFo);
changeStateNormal(ivF, tvF);
changeStateNormal(ivT, tvT);
changeStateNormal(ivS, tvS);


break;


}


}


private void changeStateNormal(ImageView iv, TextView tv) {
iv.setBackgroundResource(R.drawable.apple_black);
tv.setTextColor(getResources()
.getColor(android.R.color.background_dark));
}


private void changeStateClick(ImageView iv, TextView tv) {
iv.setBackgroundResource(R.drawable.apple_red);
tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
}


}






4,只需直接带入就可以正常使用:




 <包名.BottomNavigation
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </包名.BottomNavigation>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值