FlexboxLayout

FlexboxLayout

介绍:

FlexboxLayout 可以理解为高级的 LinearLayout,因为这两个布局都将其子视图按序排列。二者之间的重要差异在于 FlexboxLayout 具有自动 “换行” 的特性。

引入依赖:

    //flexbox布局
    implementation 'com.google.android:flexbox:1.1.0'

常用效果:

横向显示自动换行:

xml文件:

 <com.google.android.flexbox.FlexboxLayout
            android:id="@+id/fl_hot_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            app:alignContent="flex_start"
            app:alignItems="flex_start"
            app:flexDirection="row"
            app:flexWrap="wrap"
            app:justifyContent="flex_start"
            app:layout_constraintTop_toBottomOf="@+id/tv_hot_search">

         

</com.google.android.flexbox.FlexboxLayout>

动态添加控件:

   private void addView(View viewGroup, List<String> strings, View.OnClickListener onClickListener) {
        FlexboxLayout flHotSearch = activity.bindId(viewGroup, R.id.fl_hot_search);
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).isEmpty()) continue;
            TextView textView = new TextView(viewGroup.getContext());
            textView.setText(strings.get(i));
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(getDP(16), getDP(0), getDP(16), getDP(0));
            textView.setOnClickListener(onClickListener);
            //构造宽高的属性
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, getDP(32));
            layoutParams.setMargins(getDP(0), getDP(8), getDP(8), 0);
            flHotSearch.addView(textView, layoutParams);
        }
    }

    private int getDP(int px) {
        return WindowUtils.dip2px(activity, px);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值