在此记下自己是怎么解决TextView上文字太多而显示不下的问题的:
如果TextView上显示的文字太多太长,有两种方法解决:A .后面加省略号,B.滚动显示,这样文字再多都显示得下。
"abcde-0001-123456789012345",这串字符就是要处理的,怎么弄成单行显示?
我自己首先尝试加省略号的方式:
<TextView
android:id="@+id/tv_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="right"
android:singleLine="true"
android:textColor="@color/gray"
android:textSize="14sp" />
关键是设了ellipsize="end"和singleLine="true"
结果发现这个方法在三星S3这样的高分辨率、大屏幕手机上可以正常显示三个点的省略号(abcde-0001-123456789...),但是对小屏幕手机如HTC T328d,甚至Lt26ii都只显示一个点的省略号(abcde-0001-123456789.)。后来尝试在后台写代码处理,参照 http://stackoverflow.com/questions/11210553/textview-ellipsize-issue。结果在小屏幕手机上还是不行(但是如果layout_width设的是一个较小的固定值,如150dip,故意让它显示不下而分行,上述方法可行,只是写固定值在大屏上就会留出很多空白,不妥)。于是放弃,开始尝试采用用marquee来做:
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde-0001-123456789012345"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
/>
但是文字并没有滚动起来,后来又加上了android:focusable, android:maxLines,android:lines,
android:scrollHorizontally="true",
但
都不行。文字始终没有滚动起来。
后来在http://stackoverflow.com/questions/2182578/marquee-text-in-android 查到,原来要想TextView滚动起来,必须使其获得焦点,若要使其自动滚动,必须在后台代码写上这句:
tv.setSelected(true); // Set focus to the textview
【原话:Set android:ellipsize="marquee"
in TextView
element in your layout file (XML). Then, in your Activity's onCreate()
method, add the line textView.setSelected(true);】
果然,加上之后,OK了,解决。