//布局
<?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" android:orientation="horizontal"> <EditText android:id="@+id/main_ed" android:layout_width="300dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认" /> </LinearLayout> <com.bway.day_0701_demo.FlowLayout android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="match_parent"> </com.bway.day_0701_demo.FlowLayout> </LinearLayout>
//流式布局代码
public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(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); //测量 int widthSize=MeasureSpec.getSize(widthMeasureSpec); int widthMode=MeasureSpec.getMode(widthMeasureSpec); int heightSize=MeasureSpec.getSize(heightMeasureSpec); int heightMode=MeasureSpec.getMode(heightMeasureSpec); //定义属性 int wight=0; int height=0; int childWidth =0; int childHeight=0; int linWigth =0; int linHeight =0; int totaHeight =0; View viewChild; for (int i = 0; i <getChildCount() ; i++) { viewChild=getChildAt(i); //获取自布局宽高 childWidth=viewChild.getMeasuredWidth(); childHeight=viewChild.getMeasuredHeight(); if (childWidth > widthSize){ throw new IllegalArgumentException("你的长度太大"); } if (linWigth + childWidth>widthSize){ //换行 wight=widthSize; totaHeight +=linHeight; linWigth =childWidth; linHeight=childHeight; }else{ //不换行 linWigth+=childWidth; linHeight =Math.max(linHeight,childHeight); wight=Math.max(linWigth,wight); } if (i ==getChildCount()-1){ totaHeight= totaHeight+linHeight; height =totaHeight; } } wight =widthMode == MeasureSpec.EXACTLY?widthSize:wight; height=heightMode == MeasureSpec.EXACTLY?heightSize:height; setMeasuredDimension(wight,height); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childWidth =0; int childHeight=0; int linWigth =0; int linHeight =0; int totaHeight =0; View viewChild; for (int i = 0; i < getChildCount(); i++) { viewChild=getChildAt(i); childWidth=viewChild.getMeasuredWidth(); childHeight=viewChild.getMeasuredHeight(); if (linWigth +childWidth >getMeasuredWidth()){ //换行 linWigth=0; totaHeight +=linHeight; layoutView(viewChild,linWigth,totaHeight,linWigth+childWidth,totaHeight+childHeight); linHeight =childHeight; linWigth=childWidth; }else{ //不换行 layoutView(viewChild,linWigth,totaHeight,linWigth+childWidth,totaHeight+childHeight); linWigth+=childWidth; linHeight =Math.max(linHeight,childHeight); } } } private void layoutView(View viewChild, int linWidth, int totalHeight, int i, int i1) { linWidth+=getPaddingLeft(); linWidth+=getPaddingTop(); viewChild.layout(linWidth,totalHeight,i,i1); } }
//MainActivity
public class MainActivity extends AppCompatActivity { private EditText editText; private Button btn; private FlowLayout flow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { editText = findViewById(R.id.main_ed); btn = findViewById(R.id.btn); flow = findViewById(R.id.flow); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String s = editText.getText().toString(); TextView textView = new TextView(MainActivity.this); textView.setText(s); flow.addView(textView); } }); } }
本文主要探讨了如何在Android开发中实现输入框的流式布局,详细解析了相关代码实现,帮助开发者理解并掌握这种布局方式。
317

被折叠的 条评论
为什么被折叠?



