I want to know how to automatically adjust the size of the TextView
according to the length of the String
. I am getting some data from another Intent
as a String
and storing it in a TextView
. If I leave it small, the whole text won't fit and only half of it is displayed. If I leave a big space for the TextView
it does not look nice on the screen.
我想知道如何根据String的长度自动调整TextView的大小。我从另一个Intent获取一些数据作为String并将其存储在TextView中。如果我把它留小,整个文本将不适合,只显示一半。如果我为TextView留下一个很大的空间,它在屏幕上看起来不太好看。
The solution I found online was using extends TextView
and using this method gives errors in my class and I have to change a lot of functions because of this
我在网上找到的解决方案是使用扩展TextView并使用此方法在我的课程中出错,我必须更改很多功能因为这个
2 个解决方案
#1
6
Use a multi-line TextView and set layout_height
to wrap_content
:
使用多行TextView并将layout_height设置为wrap_content:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
Note: If you set layout_width
to wrap_content
it won't work properly. If you don't want the TextView
to take up the entire width of the parent view then either:
注意:如果将layout_width设置为wrap_content,它将无法正常工作。如果您不希望TextView占用父视图的整个宽度,则:
- Set it to a fixed width in XML, or
- 将其设置为XML中的固定宽度,或
- Set the width programmatically, or
- 以编程方式设置宽度,或
- Set
layout_alignLeft
/layout_alignRight
etc in aRelativeLayout
- 在RelativeLayout中设置layout_alignLeft / layout_alignRight等
- Set the
layout_width
to0dp
and use alayout_weight
in aLinearLayout
- 将layout_width设置为0dp并在LinearLayout中使用layout_weight
#2
2
If you are using xml file, just do this,
如果您使用的是xml文件,请执行此操作,
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
And if you want to do dynamically do like this,
如果你想动态做这样的事,
TextView textView= new TextView(activity);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(param);
#1
6
Use a multi-line TextView and set layout_height
to wrap_content
:
使用多行TextView并将layout_height设置为wrap_content:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
Note: If you set layout_width
to wrap_content
it won't work properly. If you don't want the TextView
to take up the entire width of the parent view then either:
注意:如果将layout_width设置为wrap_content,它将无法正常工作。如果您不希望TextView占用父视图的整个宽度,则:
- Set it to a fixed width in XML, or
- 将其设置为XML中的固定宽度,或
- Set the width programmatically, or
- 以编程方式设置宽度,或
- Set
layout_alignLeft
/layout_alignRight
etc in aRelativeLayout
- 在RelativeLayout中设置layout_alignLeft / layout_alignRight等
- Set the
layout_width
to0dp
and use alayout_weight
in aLinearLayout
- 将layout_width设置为0dp并在LinearLayout中使用layout_weight
#2
2
If you are using xml file, just do this,
如果您使用的是xml文件,请执行此操作,
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
And if you want to do dynamically do like this,
如果你想动态做这样的事,
TextView textView= new TextView(activity);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(param);