SoFunction
Updated on 2025-03-01

Android custom view streaming layout wraps lines according to the number of text

This article shares the specific code of Android's line breaking according to the number of texts for your reference. The specific content is as follows

//Home page Define data frame

package ;

import .;
import ;
import ;
import ;

import ;
import ;

public class MainActivity extends AppCompatActivity {
 List<String> stringList = new ArrayList<>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  initView();
 }

 private void initView() {
  final EditText editText = findViewById();
  final CustomWaterFallViewGroup customWaterFallViewGroup = findViewById(.water_fill);
  findViewById().setOnClickListener(new () {
   @Override
   public void onClick(View v) {
    //Get the value of the input box    String str = ().toString();
    //Put text into the list    (str);
    //Set data    (stringList);
   }
  });
 }
}

//zhuye layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 xmlns:app="/apk/res-auto"
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:
  android:hint="enter"
  />
 <Button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:
  android:text="add"
  />
 <
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:
  />
</LinearLayout>

//Custom streaming layout

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

public class CustomWaterFallViewGroup extends LinearLayout {
 //Set the maximum length of the string for each line int mMaxSize = 22;
 //Passed in string array List<String> stringList = new ArrayList<>();
 Context mcontext;

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

 public CustomWaterFallViewGroup(Context context,AttributeSet attrs) {
  super(context, attrs);
  mcontext = context;
  init();
 }
 //Define the layout private void init() {
  //Set the outermost LinearLayout as a vertical layout  setOrientation(VERTICAL);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  (widthMeasureSpec, heightMeasureSpec);
  DisplayMetrics displayMetrics = ().getDisplayMetrics();
  int widthPixels = ;
  setMeasuredDimension(widthPixels,heightMeasureSpec);
 }

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  (changed, l, t, r, b);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  (canvas);
 }

 public void setData(List<String> stringList) {
  //The data in the previous input box is stored in the collection of this page   = stringList;
  showData();
 }

 private void showData() {
  //Because you have to re-draw every time, remove the previous layout and display the updated layout  removeAllViews();
  //Preferring to the following layout, add a horizontal layout  LinearLayout linearLayout_h = (LinearLayout) (mcontext,.item_water_fall_h,null);
  addView(linearLayout_h);
  //Define temporary variables.  Used to calculate the length of the characters on the last line  int len = 0;
  for (int i = 0;i<();i++){
   String str = (i);
   //Add the second string length with the existing string length of the record   len += ();
   //-Judgement If it is greater than the maximum length, it means that this line cannot be put down   //Requires automatic wrapping   if (len > mMaxSize){
    //Add a horizontal layout like the layout    linearLayout_h = (LinearLayout) (mcontext,.item_water_fall_h,null);
    addView(linearLayout_h);
    //After the line break, it is not added, so the current rescue is the length of the last line.    len = ();
   }
   //Add a textView control   View view = (mcontext,.water_fall_textview,null);
   //Get its ID   TextView textView = (.water_fall_textview);
   //After getting it, assign it a value (give it the value in the input box)   (str);
   //Add to layout   linearLayout_h.addView(view);

   //Set weights Let all controls in each line add up to fill the entire line and allocate it reasonably    layoutParams = () ();
    = 1;
   (layoutParams);

   final int index = i;
   (new OnClickListener() {
    @Override
    public void onClick(View v) {
     (mcontext,"You clicked"+(index),Toast.LENGTH_SHORT).show();
    }
   });
   (new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
     (index);
     showData();
     return false;
    }
   });
  }
 }
}

//Layout of each line

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

</LinearLayout>

//Flowing layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 >

 <TextView
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/colorAccent"
  android:layout_weight="1"
  android:textSize="20dp"
  android:layout_marginRight="5dp"
  android:layout_marginLeft="5dp"
  android:layout_marginTop="10dp"
  android:gravity="center"
  />
</LinearLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.