0

我有一個非常簡單的佈局(線性)爲我的ListView項目,只有3小部件水平佈局:2 TextView項目,然後按鈕。但即使所有3的layout_height都是wrap_content,看起來這個按鈕所使用的值強制ListItem比需要的擴展更多。但只需將layout_height的值更改爲25dp即可消除此問題。當layout_height更改爲wrap_content時,爲什麼ListItem(LinearLayout)的高度如此之高?

爲什麼Button的wrap_content有這種效果,以及如何阻止它?

請注意我的屏幕截圖中的背景顏色,顯示小部件尺寸。 只有這兩個圖片的區別在於,在我的XML(底部)Button的layout_height已經從wrap_content更改爲25dp。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 

    <TextView 
     android:id="@+id/tvTotal" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:layout_gravity="left" 
     android:text="0." 
     android:textSize="18sp" 
     android:maxLines="1" 
     android:layout_weight=".15" 
     android:background="@color/LightGreen" 
     /> 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:text="" 
     android:textSize="18sp" 
     android:maxLines="1" 
     android:layout_weight=".55" 
     android:background="@color/LightBlue" 
     /> 

    <Button 
     android:id="@+id/btnMyButton" 
     android:layout_width="0dp" 
     android:layout_height="25dp" 
     android:gravity="right" 
     android:layout_gravity="right" 
     android:textColor="@color/CornflowerBlue" 
     android:text="Questions" 
     android:textSize="16dp" 
     android:maxLines="1" 
     android:layout_weight=".3" 
     android:background="@color/black" 
     > 
    </Button> 

</LinearLayout> 

對巴頓的layout_height使用WRAP_CONTENT:

enter image description here

硬編碼25dp爲巴頓的layout_height:

enter image description here

回答

1

額外的空間是按鈕默認填充。

如果你想刪除它使用:

​​

在你的代碼

<Button 
    android:id="@+id/btnMyButton" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:layout_weight=".3" 
    android:background="@color/black" 
    android:gravity="right" 
    android:maxLines="1" 
    android:minHeight="0dp" 
    android:minWidth="0dp" 
    android:text="Questions" 
    android:textColor="@color/CornflowerBlue" 
    android:textSize="16dp"></Button> 
+0

果然。離奇。 Android爲什麼會插入這麼多的填充,還有爲什麼在Button上卻沒有其他的小部件? – Alyoshak

+0

因爲我猜Ux問題。原因按鈕的設計是可點擊的..和可見文本里面..所以默認填充添加。 – rafsanahmad007

+0

這是很多默認的填充。 – Alyoshak

相關問題