Android 中TextView的使用imageview被壓縮問題解決辦法
Android 中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" />
運行效果就正常了:
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
利用Jetpack Compose實現(xiàn)主題切換功能
這篇文章主要介紹了如何利用Android中的Jetpack Compose實現(xiàn)主題切換功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-01-01Android中ListView的item點擊沒有反應(yīng)的解決方法
這篇文章主要介紹了Android中ListView的item點擊沒有反應(yīng)的相關(guān)資料,需要的朋友可以參考下2017-10-10Android AIDL實現(xiàn)跨進程通信的示例代碼
本篇文章主要介紹了Android AIDL實現(xiàn)跨進程通信的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08Android實現(xiàn)濾鏡效果ColorMatrix
這篇文章主要為大家詳細介紹了Android實現(xiàn)濾鏡效果ColorMatrix,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實例代碼)
項目是要保存上次文件播放的位置,我使用SharedPreferences來保存,鍵值對分別是文件路徑和當(dāng)時播放的位置2013-11-11CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理
今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Android啟動初始化方案App StartUp的應(yīng)用詳解
這篇文章主要介紹了Android啟動初始化方案App StartUp的使用方法,StartUp是為了App的啟動提供的一套簡單、高效的初始化方案,下面我們來詳細了解2022-09-09