自定义view流式布局

自定义view流式布局 已经封装依赖可以直接粘贴使用
1.导入依赖

implementation 'com.github.LiHangKun:LiuShiBuJu:1'

然后在项目的build.gradle中

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//要添加的
    }
}

2.布局文件 直接使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="100dp"
            android:id="@+id/et"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/bt"
            android:text="sousuo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
   <com.bw.liushibuju2.FlowLayout
       android:id="@+id/aaa"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

   </com.bw.liushibuju2.FlowLayout>

</LinearLayout>

3.在类里面

public class MainActivity extends AppCompatActivity {
    List<String> stringList = new ArrayList<>();
    private FlowLayout mFl;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        mFl = findViewById(R.id.aaa);
        et = findViewById(R.id.et);
        //点击搜索
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String gettx = et.getText().toString();
                stringList.add(gettx);
                //清除
                mFl.removeAllViews();
                for(String string : stringList){
               //遍历 并添加TexView
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(10, 5, 10, 5);
                    TextView textView = new TextView(MainActivity.this);
                    textView.setPadding(20, 10, 20, 10);
                    textView.setText(string);
                    textView.setLayoutParams(layoutParams);
				
                    mFl.addView(textView, layoutParams);
                }
            }
        });

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值