流式布局(根据字符串长度)

MainActivity

public class MainActivity extends AppCompatActivity {

    List<String> strList=new ArrayList<>();
    private CustomView customView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        final EditText editText = findViewById(R.id.edit);
        customView = findViewById(R.id.water_fall);
        //customView =new CustomView(MainActivity.this);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入框文字
                String str = editText.getText().toString();
                // 将文字放入列表
                strList.add(str);
                // 设置数据
                customView.setStringList(strList);
            }
        });
    }
}

自定义view

public class CustomView extends LinearLayout {

    //规定一行的字符串长度不能大于30  大于30就换行
    final int mMaxSize=30;
    List<String> stringList=new ArrayList<>();
    Context mcontext;
    private TextView textView;
    private View view;

    public CustomView(Context context) {
        super(context);
        mcontext=context;
        init();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mcontext=context;
        init();
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
        showList();
    }

    private void showList() {
        //因为每一次都要重新画,所以移除以前所有的布局
        removeAllViews();
        // 优先向根布局添加一条横向布局
        LinearLayout linearLayout_h= (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
        addView(linearLayout_h);
        // 定义临时变量,用来计算最后一行已有的字符串长度
        int len=0;

        for(int i=0;i<stringList.size();i++){
            // 将此字符串长度与记录的已有字符串长度相加
            len+=stringList.get(i).length();
            // 如果长度大于规定最大长度,说明这一行放不下了,需要换行
            if(len>mMaxSize){
                // 向根布局添加一条横向布局
                linearLayout_h= (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
                addView(linearLayout_h);
                // 因为换行,所以这一个字符串长度就是最后一行长度
                len=stringList.get(i).length();
            }
            // 添加TextView,并赋值
            View view= View.inflate(mcontext,R.layout.item_water_fall,null);
            textView = view.findViewById(R.id.tv_item_water_fall);
            textView.setText(stringList.get(i));
            linearLayout_h.addView(view);

            // 设置权重,让每一行内所有控件相加充满整行,并合理分配
            LayoutParams layoutParams= (LayoutParams) view.getLayoutParams();
            layoutParams.weight=1;
            view.setLayoutParams(layoutParams);
            final int index=i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mcontext,stringList.get(index),Toast.LENGTH_SHORT).show();
                }
            });
            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    stringList.remove(index);
                    showList();
                    return true;
                }
            });
        }

    }

    private void init() {
        // 设置最外层的LinearLayout为垂直布局
        setOrientation(VERTICAL);
    }
}

activity_main.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入"/>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="add"/>

        <com.example.water.view.CustomView
            android:id="@+id/water_fall"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.example.water.view.CustomView>

    </LinearLayout>
</ScrollView>

纵向的LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/ll_item_h"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


</LinearLayout>

横向的LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_item_water_fall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff00cc"
        android:layout_weight="1"
        android:textSize="20sp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
    </TextView>

</LinearLayout>

效果图

长按可以删除  点击可以Toast  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值