最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 中TextView的使用imageview被壓縮問(wèn)題解決辦法

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

Android 中TextView的使用imageview被壓縮問(wèn)題解決辦法

看下運(yùn)行效果圖:

今天解bug的時(shí)候遇到一個(gè)奇怪的問(wèn)題:listview的item由一個(gè)textview和一個(gè)imageview組成,父布局是線性水平排列。我的本意是imageview顯示相同的圖片,textview顯示文本,但是運(yùn)行程序后發(fā)現(xiàn),當(dāng)某個(gè)textview的文本較多時(shí),imageview會(huì)被壓縮,剛開(kāi)始沒(méi)注意,檢查代碼了好久。

代碼示例如下:

<!--文本少的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ù)過(guò)多的時(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)論

安康市| 孟连| 上林县| 临澧县| 淳化县| 宽城| 新巴尔虎左旗| 汨罗市| 屯门区| 张家港市| 威海市| 措美县| 丰宁| 华池县| 澄城县| 麻栗坡县| 吴桥县| 三都| 淳化县| 蒙山县| 金沙县| 双峰县| 农安县| 中卫市| 类乌齐县| 西吉县| 屯昌县| 溧水县| 科技| 黔西县| 天台县| 普兰县| 陇川县| 大庆市| 朝阳市| 门头沟区| 仁化县| 涿州市| 武隆县| 河西区| 永泰县|