原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1840
问题描述:
有一个多行文本的textView,如果文本不超过显示空间,就设置重力向上
复制代码
1tv.setGravity(Gravity.TOP);
如果文本过多超出范围,我需要显示文本的结尾部分。
复制代码
1tv.setGravity(Gravity.BOTTOM);
布局:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<RelativeLayout android:id="@+id/textLayout" android:layout_marginTop="16dp" android:layout_width="fill_parent" android:layout_height="110dp" android:orientation="vertical" android:visibility="visible" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:gravity="top" android:text="Long text bla bla." android:textColor="@android:color/black" android:textColorHint="@android:color/black" android:textSize="37sp" /> ... </RelativeLayout>
不知道应该怎么让应用能判断文本有多长?有没有超出范围。每个屏幕尺寸不同所以不能通过硬编码设定行数判断。
解决方案:
在LinearLayout
中加入TextVew
,然后比较TextVew 和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<RelativeLayout android:id="@+id/textLayout" android:layout_marginTop="16dp" android:layout_width="fill_parent" android:layout_height="110dp" android:orientation="vertical" android:visibility="visible" > <LinearLayout android:id ="@+id/linear" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:gravity="top" android:text="Long text bla bla." android:textColor="@android:color/black" android:textColorHint="@android:color/black" android:textSize="37sp" />
比较高度:
复制代码
1
2
3if(text.getHeight()>linear.getHeight()){ //Here TextView longer than available space }
最后
以上就是发嗲蜡烛最近收集整理的关于textView中文本过长被截断的解决方案的全部内容,更多相关textView中文本过长被截断内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复