TextView排版效果
TextView排版效果
TextView 排版效果
右侧 TextView 恒定存在, 左侧宽度自适应
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
<?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="match_parent"
android:orientation="horizontal">
<!-- 外层 LinearLayout 宽度必须为 match_parent 或固定为 xxdp -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00897B"
android:ellipsize="end"
android:maxLines="1"
android:text="我从坚实的查克拉时间长了参加擦手机电池实力差距阿实力差距"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F4511E"
android:text="我是标签"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
ConstraintLayout 实现方式
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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/place_holder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#333"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/mTvTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:background="@drawable/shape_r2_bged834d"
android:text="我是标签"
android:textColor="#ED834D"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@id/place_holder"
app:layout_constraintLeft_toRightOf="@id/tv_text"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/place_holder" />
<!--废弃: layout_constraintWidth_default="wrap" -->
<TextView
android:id="@+id/tv_text"
android:layout_width="0dp" <!--此 layout_width 需设置为 0, 否则无效果 -->
android:layout_height="wrap_content"
android:text="对外投资企业"
android:textColor="#333"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/mTvTag"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap" />
layout_constraintWidth_default="wrap" is deprecated. Use layout_width="WRAP_CONTENT" and layout_constrainedWidth="true" instead.
修改为:
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="对外投资企业"
android:textColor="#333"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/mTvTag"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedWidth="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
TextView 不同字数两端对齐
代码:
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
public class StringUtils {
/**
* 字符串中插入元素
*/
public static String insert(String str, String ele) {
return String.join(ele, str.split(""));
}
public static String align(String str, int length) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
stringBuilder.append("问");
}
System.out.println(stringBuilder);
if (TextUtils.isEmpty(str)) {
return str;
}
if (length < 3 || length > 7) {
return str;
}
if (str.length() < 2 || str.length() > 7) {
return str;
}
if (str.length() > length) {
return str;
}
switch (length) {
case 3:
if (str.length() == 2) {
return insert(str, "\u3000");
}
break;
case 4:
if (str.length() == 2) return insert(str, "\u3000\u3000");
if (str.length() == 3) return insert(str, "\u2002");
break;
case 5:
if (str.length() == 2) return insert(str, "\u3000\u3000\u3000");
if (str.length() == 3) return insert(str, "\u3000");
if (str.length() == 4) return insert(str, "\u2004");
break;
case 6:
if (str.length() == 2) return insert(str, "\u3000\u3000\u3000\u3000");
if (str.length() == 3) return insert(str, "\u3000\u2002");
if (str.length() == 4) return insert(str, "\u2004\u2004");
if (str.length() == 5) return insert(str, "\u2005");
break;
case 7:
if (str.length() == 2) return insert(str, "\u3000\u3000\u3000\u3000\u3000");
if (str.length() == 3) return insert(str, "\u3000\u3000");
if (str.length() == 4) return insert(str, "\u2002\u2002");
if (str.length() == 5) return insert(str, "\u2002");
if (str.length() == 6) return insert(str, "\u2009");
break;
default:
break;
}
return str;
}
}
测试代码:
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
private fun setTextViewAdjust() {
val layout = findViewById<LinearLayout>(R.id.ll_container)
val texts = "新闻公告研报经营"
for (j in 3..7) {
val stringBuilder = StringBuilder()
for (i in 0 until j) {
stringBuilder.append(texts[i])
}
val textView = TextView(this)
textView.textSize = 16f
textView.setBackgroundColor(Color.RED)
textView.text = stringBuilder
val lp = MarginLayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
lp.topMargin = 25
layout.addView(textView, lp)
var index = 2
while (index < j) {
val view = TextView(this)
view.textSize = 16f
val sb = StringBuilder()
for (i in 0 until index) {
sb.append(texts[i])
}
view.text = StringUtils.align(sb.toString(), j)
layout.addView(view)
index++
}
}
}
本文由作者按照 CC BY 4.0 进行授权