国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 中TextView的使用imageview被壓縮問題解決辦法

2019-12-12 03:08:43
字體:
來源:轉載
供稿:網友

Android 中TextView的使用imageview被壓縮問題解決辦法

看下運行效果圖:

今天解bug的時候遇到一個奇怪的問題:listview的item由一個textview和一個imageview組成,父布局是線性水平排列。我的本意是imageview顯示相同的圖片,textview顯示文本,但是運行程序后發現,當某個textview的文本較多時,imageview會被壓縮,剛開始沒注意,檢查代碼了好久。

代碼示例如下:

<!--文本少的item--><LinearLayout  android:id="@+id/ll"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="#e6e9ed"  android:gravity="center_vertical|right">  <TextView    android:id="@+id/title"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:gravity="bottom"    android:text="我們右邊引用的是同一張圖片"    android:textSize="16sp" /><ImageView    android:id="@+id/icon"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="17dp"    android:layout_marginRight="23dp"    android:background="@drawable/test" /></LinearLayout><!--文本多的item--><LinearLayout  android:id="@+id/ll"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="50dp"  android:background="#e6e9ed"  android:gravity="center_vertical|right">  <TextView    android:id="@+id/title"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:gravity="bottom"    android:text="我們右邊引用的是同一張圖片,我字數多"    android:textSize="16sp" /><ImageView    android:id="@+id/icon"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="17dp"    android:layout_marginRight="23dp"    android:background="@drawable/test" /></LinearLayout>

可以發現,第二個布局中,右邊圖片被“擠扁”了。為什么會出現這種情況?其實很簡單,是textview寬度自適應搞的鬼。水平線形布局中,我們雖然設置了imageview與左右的偏移(margin)值,但是由于自布局textview與imageview是按順序排列的,textview會首先完成它的自適應,導致字數過多的時候會把右邊的imageview壓縮,此時imageview的左右margin值還是我們設置的。

那么,怎么設置才能讓文本框顯示較多文字而又不擠壓右邊的imageview呢?

答案很簡單,還是要在textview的寬度上做文章了。只需要:

textview的width屬性依然設置為:wrap_content自適應,再加上一個權重屬性即可:weight="1".這樣,textview就會占據水平剩下的空間,而不會去擠壓右邊的imageivew了。

代碼如下:

<LinearLayout  android:id="@+id/ll"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="50dp"  android:background="#e6e9ed"  android:gravity="center_vertical|right">  <TextView    android:id="@+id/title"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_weight="1"    android:ellipsize="end"    android:gravity="bottom"    android:singleLine="true"    android:text="我們右邊引用的是同一張圖片,我字數多"    android:textSize="16sp" />  <ImageView    android:id="@+id/icon"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="17dp"    android:layout_marginRight="23dp"    android:background="@drawable/test" />

運行效果就正常了:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塘沽区| 临沧市| 于都县| 曲周县| 东阿县| 阳泉市| 馆陶县| 瑞安市| 静乐县| 鄄城县| 琼结县| 通海县| 汉寿县| 清河县| 武川县| 无锡市| 景谷| 容城县| 长阳| 郧西县| 晋州市| 南江县| 原阳县| 满城县| 勃利县| 丰都县| 铜川市| 鄂伦春自治旗| 婺源县| 吉木萨尔县| 泌阳县| 新邵县| 子洲县| 青川县| 德令哈市| 聊城市| 东方市| 博罗县| 苏尼特右旗| 噶尔县| 山阳县|