流式布局

本文介绍了流式布局的概念,并通过实例展示了如何在主布局中应用自定义类进行设计,主要内容包括效果图展示、主布局的结构解析以及自定义类的详细设置,帮助读者深入理解并掌握流式布局的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图
在这里插入图片描述

主布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edit"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="9"
        android:hint="请输入搜索内容" />

    <TextView
        android:id="@+id/sousuo"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:text="搜索" />
</LinearLayout>

<com.bwie.month_liushibuju.custorm.CustormLiu
    android:id="@+id/coustorm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

自定义类

public class CustormLiu extends ViewGroup {

public CustormLiu(Context context) {
    super(context);
}

public CustormLiu(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustormLiu(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    measureChildren(widthMeasureSpec, heightMeasureSpec);
}
//    重写方法

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        先获取控件的宽度
        int width = getWidth();
//        定义一个常量行数
        int row = 0;
//        子控件左边的坐标
        int disWidth = 18;
    for (int i = 0; i < getChildCount(); i++) {
        View view = getChildAt(i);
        int viewWidth = view.getMeasuredWidth();
        int viewHeight = view.getMeasuredHeight();
        if (disWidth + viewHeight > width) {
            row++;
            disWidth = 18;
        }
        /*
        view.layout(disWidth, row * viewHeight, viewWidth + disWidth, viewHeight * (row + 1));
        disWidth += viewWidth;//disWidth = disWidth+viewWidth;
         */

        view.layout(disWidth, row * viewHeight, viewWidth + disWidth, viewHeight * (row + 1));
        disWidth += viewWidth;
    }
}
}

主页面

public class MainActivity extends AppCompatActivity {

private EditText edit;
private TextView sousuo;
private CustormLiu coustorm;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
//        初始化控件
        edit = findViewById(R.id.edit);
        sousuo = findViewById(R.id.sousuo);
        coustorm = findViewById(R.id.coustorm);
//        搜索点击事件
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView = new TextView(MainActivity.this);
//                获取输入框的值
                String s = edit.getText().toString().trim();
                textView.setText(s);
                Log.i("cv","onclick:==="+s);
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                coustorm.addView(textView,params);
                Log.i("cv","onclick==="+textView);
                Log.i("cv","onclick==="+params);
            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值