您当前的位置: 首页 > 
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ConstraintLayout的流式布局Flow

沙漠一只雕得儿得儿 发布时间:2022-06-17 11:19:07 ,浏览量:0

Flow流式布局

流式布局的实现方式有很多种,比如RecycleView的StaggeredGridLayoutManager,或者自定义布局动态计算的方式等,无论哪种实现,都不如ConstraintLayout提供的Flow流式布局来的更灵活,更简单。

base_component_learn/ConstraintFlowActivity.java at master · buder-cp/base_component_learn · GitHub

这里我们进行动态添加,效果如下:

 代码非常简单,即使用动态添加布局即可:

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.helper.widget.Flow;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.ArrayList;

public class ConstraintFlowActivity extends AppCompatActivity {

    private ConstraintLayout mainLayout;
    private Flow flow;
    private int[] idsss = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
    private String[] strings = new String[]{ "拍案叫绝银汉迢迢暗度银汉迢汉迢迢暗度", "行云流水", "花花", "词语好词",
            "如霜", "翠彤翠彤翠彤", "眉飞色舞", "梦琪", "蓉", "描写友情的词",
            "优美词语集锦", "相视而笑", "笑容", "推心置腹", "喜上眉梢", "可掬", "银汉迢迢暗度", "又岂在朝朝暮暮又岂", "便胜却", "ads"};
    private ArrayList arrayList = new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.constraint_exercise);
        mainLayout = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.constraint_exercise, null);
        setContentView(mainLayout);
        for (int i = 1; i < 20; i++) {
            arrayList.add(i);
            mainLayout.addView(generateView(i));
        }
        flow = findViewById(R.id.constrain_flows);
//        flow.setMaxElementsWrap(3);
//        flow.setHorizontalAlign();
        flow.setWrapMode(1);
        flow.setReferencedIds(idsss);
    }

    private TextView generateView(int i) {
        TextView textView = new TextView(getApplicationContext());
        textView.setId(i);
        textView.setPadding(12, 5, 12, 5);
        textView.setTextColor(Color.BLACK);
        textView.setBackgroundResource(R.drawable.vip_course_selector_bg);
        textView.setText(strings[i]);
        return textView;
    }
}

XML代码仅包含一个flow即可:




    

使用原生代码写flow流式布局是不是很简单呢。

具体Flow的一些具体属性可以看ConstraintLayout 2.0新特性解析(一)--Flow流式布局 - 简书

核心就是使用属性:app:constraint_referenced_ids将7个ImageView约束起来。




    

    

    

    

    

    

    

    

关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0371s