Android中TextView內容過長未顯示省略號的問題
阿新 • • 發佈:2018-11-04
問題描述:
按照UI設計,某個頁面展示資料超長時,需要顯示出省略號。
預期展示(有省略號):
實際展示(無省略號):
具體程式碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" android:text=" " /> <View android:id="@+id/view" android:layout_width="1dp" android:layout_height="19dp" android:layout_weight="1" android:background="@color/colorAccent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:text="測試測試測試測試測試測試測試測試測試測試測試測試測試測試" /> </LinearLayout>
原因分析:
在同事的協助下,終於發現了程式碼存在的問題。上面用作分隔用的View中多了坑爹的android:layout_weight="1"
。關於layout_weight的原理,可以參考連結。它會導致最後將剩餘的控制元件分配給該控制元件,導致了TextView的省略號顯示不出來了。
PS:不過這個地方還有一個疑問,如果前面第一個TextView刪除掉的話,則不會存在此問題。
解決方案:
刪除View定義中多處的layout_weight屬性。