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

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-23 18:31:52
字體:
供稿:網(wǎng)友

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

看下運行效果圖:

Android,TextView,被壓縮問題,TextView用imageview

今天解bug的時候遇到一個奇怪的問題:listview的item由一個textview和一個imageview組成,父布局是線性水平排列。我的本意是imageview顯示相同的圖片,textview顯示文本,但是運行程序后發(fā)現(xiàn),當(dāng)某個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="我們右邊引用的是同一張圖片,我字?jǐn)?shù)多"    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>

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

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

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

textview的width屬性依然設(shè)置為:wrap_content自適應(yīng),再加上一個權(quán)重屬性即可:weight="1".這樣,textview就會占據(jù)水平剩下的空間,而不會去擠壓右邊的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="我們右邊引用的是同一張圖片,我字?jǐn)?shù)多"    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" />

運行效果就正常了:

Android,TextView,被壓縮問題,TextView用imageview

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


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 河津市| 锡林郭勒盟| 扎兰屯市| 南川市| 满城县| 嘉峪关市| 介休市| 东源县| 桃园县| 瓮安县| 望江县| 唐河县| 建阳市| 东兰县| 泗阳县| 江川县| 砚山县| 鄯善县| 新平| 乐东| 尤溪县| 金寨县| 芦溪县| 巴林左旗| 都江堰市| 永丰县| 广河县| 上栗县| 灵寿县| 怀来县| 丰县| 和硕县| 武定县| 洱源县| 马公市| 鄂托克前旗| 望江县| 亳州市| 库尔勒市| 咸阳市| 祁门县|