Android 中TextView的使用imageview被壓縮問題解決辦法
Android 中TextView的使用imageview被壓縮問題解決辦法
看下運(yùn)行效果圖:
今天解bug的時(shí)候遇到一個(gè)奇怪的問題:listview的item由一個(gè)textview和一個(gè)imageview組成,父布局是線性水平排列。我的本意是imageview顯示相同的圖片,textview顯示文本,但是運(yùn)行程序后發(fā)現(xiàn),當(dāng)某個(gè)textview的文本較多時(shí),imageview會(huì)被壓縮,剛開始沒注意,檢查代碼了好久。
代碼示例如下:
<!--文本少的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),第二個(gè)布局中,右邊圖片被“擠扁”了。為什么會(huì)出現(xiàn)這種情況?其實(shí)很簡(jiǎn)單,是textview寬度自適應(yīng)搞的鬼。水平線形布局中,我們雖然設(shè)置了imageview與左右的偏移(margin)值,但是由于自布局textview與imageview是按順序排列的,textview會(huì)首先完成它的自適應(yīng),導(dǎo)致字?jǐn)?shù)過多的時(shí)候會(huì)把右邊的imageview壓縮,此時(shí)imageview的左右margin值還是我們?cè)O(shè)置的。
那么,怎么設(shè)置才能讓文本框顯示較多文字而又不擠壓右邊的imageview呢?
答案很簡(jiǎn)單,還是要在textview的寬度上做文章了。只需要:
textview的width屬性依然設(shè)置為:wrap_content自適應(yīng),再加上一個(gè)權(quán)重屬性即可:weight="1".這樣,textview就會(huì)占據(jù)水平剩下的空間,而不會(huì)去擠壓右邊的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" />
運(yùn)行效果就正常了:
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
利用Jetpack Compose實(shí)現(xiàn)主題切換功能
這篇文章主要介紹了如何利用Android中的Jetpack Compose實(shí)現(xiàn)主題切換功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-01-01Android中ListView的item點(diǎn)擊沒有反應(yīng)的解決方法
這篇文章主要介紹了Android中ListView的item點(diǎn)擊沒有反應(yīng)的相關(guān)資料,需要的朋友可以參考下2017-10-10Android AIDL實(shí)現(xiàn)跨進(jìn)程通信的示例代碼
本篇文章主要介紹了Android AIDL實(shí)現(xiàn)跨進(jìn)程通信的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android實(shí)現(xiàn)下載進(jìn)度條效果
vivo商店在下載應(yīng)用的時(shí)候,底部有一個(gè)圓角矩形的下載進(jìn)度條,中間有一個(gè)進(jìn)度文字,而且進(jìn)度和文字交匯的時(shí)候,交匯部分的文字會(huì)從藍(lán)色邊為白色,會(huì)有一種一半白色字,一半藍(lán)色字的效果。本文將仿照該樣式實(shí)現(xiàn)一個(gè)2021-06-06Android實(shí)現(xiàn)濾鏡效果ColorMatrix
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)濾鏡效果ColorMatrix,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實(shí)例代碼)
項(xiàng)目是要保存上次文件播放的位置,我使用SharedPreferences來保存,鍵值對(duì)分別是文件路徑和當(dāng)時(shí)播放的位置2013-11-11CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理
今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Android啟動(dòng)初始化方案App StartUp的應(yīng)用詳解
這篇文章主要介紹了Android啟動(dòng)初始化方案App StartUp的使用方法,StartUp是為了App的啟動(dòng)提供的一套簡(jiǎn)單、高效的初始化方案,下面我們來詳細(xì)了解2022-09-09