Android Custom View Streaming Layout Wrap Lines According to Number of Text
- 2021-10-25 07:47:26
- OfStack
In this paper, we share the specific code of Android according to the number of words, for your reference, the specific contents are as follows
//Home Definition Data Box
package com.example.customwaterfallviewgroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<String> stringList = new ArrayList<>();
@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);
final CustomWaterFallViewGroup customWaterFallViewGroup = findViewById(R.id.water_fill);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Gets the value of the input box
String str = editText.getText().toString();
// Put text into the list
stringList.add(str);
// Setting data
customWaterFallViewGroup.setData(stringList);
}
});
}
}
//zhuye Layout
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:hint=" Input "
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="add"
/>
<com.example.customwaterfallviewgroup.CustomWaterFallViewGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/water_fill"
/>
</LinearLayout>
//Custom streaming layout
package com.example.customwaterfallviewgroup;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustomWaterFallViewGroup extends LinearLayout {
// Set every 1 The length of the string with the largest line
int mMaxSize = 22;
// An array of strings passed in
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 Layout
private void init() {
// Set the outermost LinearLayout Vertical layout
setOrientation(VERTICAL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
DisplayMetrics displayMetrics = mcontext.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
setMeasuredDimension(widthPixels,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void setData(List<String> stringList) {
// Upper 1 The data in the input boxes is stored in the collection of this page
this.stringList = stringList;
showData();
}
private void showData() {
// Because every 1 I have to redraw it every time So remove the previous layout Show updated layout
removeAllViews();
// Adds to the heel layout first 1 Strip horizontal layout
LinearLayout linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
addView(linearLayout_h);
// Define temporary variables. Used to calculate the final 1 Existing character length of line
int len = 0;
for (int i = 0;i<stringList.size();i++){
String str = stringList.get(i);
// Add the secondary string length to the recorded existing string length
len += str.length();
//- Judge If it is greater than the maximum length, it means that this 1 I can't let go of the line
// Word wrapping is required
if (len > mMaxSize){
// Add image and layout 1 Strip horizontal layout
linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h,null);
addView(linearLayout_h);
// After the line break, because it is not added, So The current rescue is the last 1 Length of line
len = str.length();
}
// Add 1 A textView Control
View view = View.inflate(mcontext,R.layout.water_fall_textview,null);
// Object that gets it ID
TextView textView = view.findViewById(R.id.water_fall_textview);
// Assign a value to it after getting it (The value in the input box Give it)
textView.setText(str);
// Add to the layout
linearLayout_h.addView(view);
// Setting Weights Let every 1 All the controls in the row add up to fill the whole row and allocate them reasonably
LinearLayout.LayoutParams layoutParams = (LinearLayout.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," You clicked "+stringList.get(index),Toast.LENGTH_SHORT).show();
}
});
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
stringList.remove(index);
showData();
return false;
}
});
}
}
}
//Layout per 1 row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/water_fall_h"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
//Streaming layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/water_fall_textview"
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>