Android自定义底部导航栏,链式封装多种样式

这篇博客介绍了如何在Android中自定义底部导航栏,通过继承LinearLayout并进行链式封装,动态创建TextView和ImageView,实现不同样式的底部导航栏。关键在于设置android:clipChildren="false"以使中间按钮突出显示。此方法简化了底部导航栏的开发过程。
摘要由CSDN通过智能技术生成

1.实现原理

通过继承LinearLayout实现水平布局,再动态新建一个垂直布局的linearLayout,根据传入的标题名称的长度动态创建TextView和ImageView,根据顺序加到linearlayout中。

2.主类BottomView继承LinearLayout

public class BottomView extends LinearLayout {
    private TextView textView;
    private List<String> name;
    private List<Object> image;
    private List<Object> images;
    private Context context;
    private LinearLayout linearLayout;
    private ImageView imageView;
    private int color;
    private int size = 10;
    private int checkColor;
    private int width = 80;
    private int height = 80;
    private List<LinearLayout> linearLayouts;
    private List<ImageView> imageViews;
    private List<TextView> textViews;
    private boolean open = true;
    private boolean square = true;
    private Object imageContet;
    private int imageWidth = 200;
    private int imageHeight = 200;
    private OnClick onClick;
    private int imageBottom=20;

    public void setOnClick(BottomView.OnClick onClick) {
        this.onClick = onClick;
    }

    public BottomView(Context context) {
        super(context);
        this.context = context;
    }

    public BottomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public BottomView init() {
        //设置垂直linearlayout的布局
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;
        LayoutParams imageLayoutParams = new LayoutParams(width, height);
        imageLayoutParams.gravity = Gravity.CENTER;

        imageViews = new ArrayList<>();
        linearLayouts = new ArrayList<>();
        textViews = new ArrayList<>();
        this.setClipChildren(false);
        //判断是否要打开中间按钮
        if (open) {
            for (int i = 0; i < name.size(); i++) {
                final int finalI = i;
                //判断view的个数,是奇数即使打开了中间按钮也不起作用
                if ((name.size() % 2) != 0) {
                    textView = new TextView(context);
                    textView.setText(name.get(i));
                    textView.setGravity(Gravity.CENTER_HORIZONTAL);
                    textView.setTextColor(color);
                    textView.setTextSize(size);
                    textViews.add(textView);

                    imageView = new ImageView(context);
                    Glide.with(context).load(image.get(i)).into(imageView);
                    imageVi
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值