欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年04月10日 14:27:31   投稿:lqh  
這篇文章主要介紹了Android 中TextView的使用imageview被壓縮問題解決辦法的相關(guān)資料,需要的朋友可以參考下

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)文章

最新評(píng)論