本文实例为大家分享了Android根据文字数量换行的具体代码,供大家参考,具体内容如下
//主页 定义数据框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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) { //获取输入框的值 String str = editText.getText().toString(); //将文字放入列表 stringList.add(str); //设置数据 customWaterFallViewGroup.setData(stringList); } }); } } |
//zhuye 布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? 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 = "输入" /> < 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 > |
//自定义流式布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
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 { //设置每一行最大的字符串的长度 int mMaxSize = 22 ; //传入的字符串数组 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(); } //定义布局 private void init() { //设置最外层的LinearLayout 为垂直布局 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) { //上一个输入框里的数据存到这个页面的集合中 this .stringList = stringList; showData(); } private void showData() { //因为每一次都要重新画 ,所以移除之前的布局 显示更新过的布局 removeAllViews(); //优先向跟布局添加一条横向布局 LinearLayout linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h, null ); addView(linearLayout_h); //定义临时变量。用来计算最后一行已有的字符长度 int len = 0 ; for ( int i = 0 ;i<stringList.size();i++){ String str = stringList.get(i); //将次字符串长度与记录的已有字符串长度相加 len += str.length(); //-判断 如果大于最大长度,说明这一行放不下了 //需要自动换行 if (len > mMaxSize){ //像跟布局添加一条横布局 linearLayout_h = (LinearLayout) View.inflate(mcontext,R.layout.item_water_fall_h, null ); addView(linearLayout_h); //换行以后因为不添加了 所以 当前的救是最后一行的长度 len = str.length(); } //添加一个textView控件 View view = View.inflate(mcontext,R.layout.water_fall_textview, null ); //获取到它的ID TextView textView = view.findViewById(R.id.water_fall_textview); //得到后给它赋值 (输入框里的值 给它) textView.setText(str); //添加到布局中 linearLayout_h.addView(view); //设置权重 让每一行内所有的控件相加充满整行,并合理分配 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, "您点击了" +stringList.get(index),Toast.LENGTH_SHORT).show(); } }); view.setOnLongClickListener( new OnLongClickListener() { @Override public boolean onLongClick(View v) { stringList.remove(index); showData(); return false ; } }); } } } |
//每一行的布局
1
2
3
4
5
6
7
8
|
<? 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 > |
//流式布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? 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 > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43588945/article/details/84726662