流式布局

流式布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

<com.bw.qgs.liu2.LiuShi
    android:id="@+id/liu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    />

</android.support.constraint.ConstraintLayout>

public class LiuShi extends LinearLayout {

private int mHeightPixels;
private int mWidthPixels;

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

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

public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //改为垂直状态
    setOrientation(VERTICAL);
    //获取屏幕宽高
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    mWidthPixels = displayMetrics.widthPixels;
    //高度可以省略不计
    mHeightPixels = displayMetrics.heightPixels;
}
public void setAdapter(String[] data){
    removeAllViews();
    innerSetAdapter(data);
}

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

private void innerSetAdapter(String[] data) {
    LinearLayout linearLayout = iniHorl();
    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;
        }
        TextView textView = iniTextView();
        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(mWidthPixels>=totalWith+measuredWidth+params.leftMargin){
            linearLayout.addView(textView,params);
        }else{
            linearLayout = iniHorl();
            linearLayout.addView(textView,params);
        }
    }
}

private TextView iniTextView() {
    TextView textView = new TextView(getContext());
    textView.setTextColor(Color.RED);
    textView.setTextSize(20);
    return textView;
}

private LinearLayout iniHorl() {
    LinearLayout linearLayout = new LinearLayout(getContext());
    linearLayout.setOrientation(HORIZONTAL);
    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
    this.addView(linearLayout,layoutParams);
    return linearLayout;
}

}

public class MainActivity extends AppCompatActivity {

private String[] desc = new String[]{"优秀","邯郸学步","优秀","邯郸学步","东施效颦","东施效颦","优秀","邯郸学步","东施效颦",
        "优秀","邯郸学步","东施效颦","优秀","优秀","邯郸学步","东施效颦","邯郸学步","东施效颦","优秀","邯郸学步","东施效颦","优秀","邯郸学步","东施效颦"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LiuShi liu = findViewById(R.id.liu);
    liu.setAdapter(desc);
    liu.add(desc);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值