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:
硬编码25dp为巴顿的layout_height:
果然。离奇。 Android为什么会插入这么多的填充,还有为什么在Button上却没有其他的小部件? – Alyoshak
因为我猜Ux问题。原因按钮的设计是可点击的..和可见文本里面..所以默认填充添加。 – rafsanahmad007
这是很多默认的填充。 – Alyoshak