Android 自定义view之实现底部导航栏

Android实现底部导航栏需要在attrs文件中添加如下代码:

<declare-styleable name="BottomLinearLayout">
    <attr name="selectorTextColor" format="color"/>
    <attr name="defaultTextColor" format="color"/>
    <attr name="defaultIconSize" format="dimension"/>
</declare-styleable>

下面是实现具体功能代码:

public class BottomLinearLayout extends LinearLayout{
    private int defaultTextColor , selectorTextColor ;
    private float defaultIconSize;
    private OnClickBottomListener mOnClickBottomListener;
    private List<ImageView> imageViewList = new ArrayList<>();
    private List<TextView> textViewList = new ArrayList<>();
    private int[] icons , selectorIcons;
    private String[] texts;

    public BottomLinearLayout(Context context) {
        this(context , null);
    }

    public BottomLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs , 0);
    }

    public BottomLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs , R.styleable.BottomLinearLayout);
        defaultTextColor = a.getColor(R.styleable.BottomLinearLayout_defaultTextColor , Color.parseColor("#333333"));
        selectorTextColor = a.getColor(R.styleable.BottomLinearLayout_selectorTextColor , Color.RED);
        defaultIconSize = a.getDimension(R.styleable.BottomLinearLayout_defaultIconSize , ScreenUtil.dip2Px(context , 40));
        initView();
    }

    private void initView(){
        setOrientation(LinearLayout.HORIZONTAL);
    }

    //添加图标和文字
    public void addIconAndText(final int[] icons , final int[]selectorIcons , String[] texts){
        if(icons != null && texts != null && selectorIcons != null){
            this.icons = icons;
            this.selectorIcons = selectorIcons;
            this.texts = texts;
            updateAddViews();
        }
    }

    //天剑的icon和文字转成view
    private void updateAddViews(){
        if(icons.length == texts.length && icons.length == selectorIcons.length){
            int length = icons.length;
            removeAllViews();
            imageViewList.clear();
            textViewList.clear();
            for(int i = 0 ; i < length ; i ++){
                View childView = getItemView(icons[i] , texts[i]);
                addView(childView);
                if(i == 0){
                    setSelectorPosition(0);
                }
                final int finalI = i;
                childView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setSelectorPosition(finalI);
                        if(mOnClickBottomListener != null){
                            mOnClickBottomListener.clickView(v , finalI);
                        }
                    }
                });
            }
        }
    }

    //设置选中的第几个position
    public void setSelectorPosition(int position){
        updateTextColor(position);
        updateIcon(icons , selectorIcons , position);
    }

    private void updateTextColor(int position){
        if(textViewList.size() > position){
            for(int i = 0 ; i < textViewList.size() ; i ++){
                textViewList.get(i).setTextColor(defaultTextColor);
            }
            textViewList.get(position).setTextColor(selectorTextColor);
        }
    }

    private void updateIcon(int[] icons , int[] selectorIcons , int position){
        if(imageViewList.size() > position){
            for(int i  = 0 ; i < imageViewList.size() ; i ++){
                imageViewList.get(i).setImageResource(icons[i]);
            }
            imageViewList.get(position).setImageResource(selectorIcons[position]);
        }
    }

    //获取第几个item的view
    private View getItemView(int icon , String text){
        LinearLayout ll = new LinearLayout(getContext());
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0 , ViewGroup.LayoutParams.MATCH_PARENT);
        lp.weight = 1;
        lp.gravity = Gravity.CENTER;
        ll.setLayoutParams(lp);

        ImageView iv = new ImageView(getContext());
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.
                LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT);
        rlp.width = (int) defaultIconSize;
        rlp.height = (int) defaultIconSize;
        iv.setLayoutParams(rlp);
        iv.setImageResource(icon);
        ll.addView(iv);

        TextView tv = new TextView(getContext());
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setText(text);
        tv.setTextColor(defaultTextColor);
        tv.setTextSize(14);
        ll.addView(tv);
        imageViewList.add(iv);
        textViewList.add(tv);
        return ll;
    }

    public void setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        mOnClickBottomListener = onClickBottomListener;
    }


    public interface OnClickBottomListener{
        void clickView(View v , int position);
    }

}

只需要在代码中调用如下方法即可

String[] texts = new String[]{"测试" , "测试1" , "测试2" , "测试3"};//这个是底部的文字
int[] icons = new int[]{R.mipmap.ic_new_phone_before , R.mipmap.ic_new_sms_before ,
        R.mipmap.ic_new_safe_before , R.mipmap.ic_new_me_before};//这个是底部未选中图标
int[] selectorIcons = new int[]{R.mipmap.ic_new_phone_blue , R.mipmap.ic_new_sms_blue ,
        R.mipmap.ic_new_safe_blue , R.mipmap.ic_new_me_blue};//这个是底部选中的图标
bll.addIconAndText(icons , selectorIcons , texts);
bll.setOnClickBottomListener(new BottomLinearLayout.OnClickBottomListener() {
    @Override
    public void clickView(View v, int position) {
        //这个是显示底部的item的点击事件
        LogUtils.e(TAG , "position : " + position);
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值