android FlexboxLayout可伸缩布局

android FlexboxLayout可伸缩布局_第1张图片
1、app添加依赖

 //响应式伸缩性布局
    implementation 'com.google.android:flexbox:0.2.2'

2、MainActivity

public class MainActivity extends AppCompatActivity {
private FlexboxLayout flexboxLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow);
        String[] tags = {"婚姻育儿", "散文", "设计", "上班这点事儿", "影视天堂", "大学生活", "美人说", "运动和健身", "工具癖", "生活家", "程序员", "想法", "短篇小说", "美食", "教育", "心理", "奇思妙想", "美食", "摄影"};
        flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
        for (int i = 0; i < tags.length; i++) {
            Book model = new Book();
            model.setId(i);
            model.setName(tags[i]);
            flexboxLayout.addView(createNewFlexItemTextView(model));
        }

    }
/**
 * 动态创建TextView
 * @param book
 * @return
 */
private TextView createNewFlexItemTextView(final Book book) {
    TextView textView = new TextView(this);
    textView.setGravity(Gravity.CENTER);
    textView.setText(book.getName());
    textView.setTextSize(12);
    textView.setTextColor(getResources().getColor(colorAccent));
    //textView.setBackgroundResource(R.drawable.aa);
    textView.setTag(book.getId());
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("fenxi", book.getName());
            view.setBackgroundColor(getResources().getColor(colorPrimary));
        }
    });
    int padding = Util.dpToPixel(this, 4);
    int paddingLeftAndRight = Util.dpToPixel(this, 8);
    ViewCompat.setPaddingRelative(textView, paddingLeftAndRight, padding, paddingLeftAndRight, padding);
    FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    int margin = Util.dpToPixel(this, 6);
    int marginTop = Util.dpToPixel(this, 16);
    layoutParams.setMargins(margin, marginTop, margin, 0);
    textView.setLayoutParams(layoutParams);
    return textView;
}
}

3、工具类Util

public class Util {
    public static int pixelToDp(Context context, int pixel) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return pixel < 0 ? pixel : Math.round(pixel / displayMetrics.density);
    }
    public static int dpToPixel(Context context, int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return dp < 0 ? dp : Math.round(dp * displayMetrics.density);
    }
}

4、实体类Book

public class Book {
    private int id;
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Book() {
    }
}

5、XML



    


你可能感兴趣的:(android)