流式布局的简单使用

布局文件

<com.cn.jyx.floatlayout.FloatLayout
    android:id="@+id/floatlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.cn.jyx.floatlayout.FloatLayout>

具体实现

public class FloatLayout extends LinearLayout {
private int screenWidth;
private int screenHeight;

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

public FloatLayout(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
}

public FloatLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //改方向为垂直
    setOrientation(VERTICAL);
    //获取屏幕的宽和高
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    screenWidth = displayMetrics.widthPixels;
    screenHeight = displayMetrics.heightPixels;
}

public void setAdapter(String[] data) {
    //去掉所有的子view
    removeAllViews();
    initSetAdapter(data);
}

public void add(String[] data) {
    initSetAdapter(data);
}

private void initSetAdapter(String[] data) {
    LinearLayout linearLayout = initHor();
    for (int i = 0; i < data.length; i++) {
        String temp = data[i];
        int childCount = linearLayout.getChildCount();
        int totalWith = 0;
        for (int j = 0; j < childCount; j++) {
            int measuredWidth = linearLayout.getMeasuredWidth();
            TextView childAt = (TextView) linearLayout.getChildAt(j);
            //一定要调用一次测量方法
            childAt.measure(getMeasuredWidth(), getMeasuredHeight());

            int width = childAt.getMeasuredWidth();
            LinearLayout.LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();

            totalWith += width + layoutParams.leftMargin;
        }
        final TextView textView = initTextView();
        LinearLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        params.leftMargin = 10;
        params.topMargin = 10;
        params.bottomMargin = 10;
        textView.setText(temp);

        textView.measure(getMeasuredWidth(), getMeasuredHeight());
        int measuredWidth = textView.getMeasuredWidth();
        if (screenWidth >= totalWith + measuredWidth + params.leftMargin) {
            linearLayout.addView(textView, params);
        } else {
            //不能新声明变量
            linearLayout = initHor();
            linearLayout.addView(textView, params);
        }
    }
}

private LinearLayout initHor() {
    LinearLayout linearLayout = new LinearLayout(getContext());
    linearLayout.setOrientation(HORIZONTAL);
    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    this.addView(linearLayout,params);
    return linearLayout;
}
private TextView initTextView(){
    TextView textView = new TextView(getContext());
    textView.setTextColor(Color.parseColor("#FF0000"));
    textView.setTextSize(20);
    return textView;
}
}

调用方法

public class MainActivity extends AppCompatActivity {
//自定义数据
private String[] desc = new String[]{"兰蔻","香奈儿","芭比波朗","SK-II","科颜氏","理肤泉","赫莲娜","纪梵希","雅诗兰黛","迪奥","肌肤之钥","馥蕾诗","欧莱雅","Olay"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FloatLayout floatLayout=findViewById(R.id.floatlayout);
    floatLayout.setAdapter(desc);
    //floatLayout.add(desc);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值