自定义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);
                }
            }
        });

    }
}

你可能感兴趣的:(自定义view)