我的按钮定义如下:
<Button
android:drawableTop="@drawable/ico"
android:gravity="center"
android:id="@+id/StartButton2x2"
android:text="@string/Widget2x2StartBtnLabel"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/widgetbtn_bg"
/>
问题是“drawableTop”图像与 Button View 的顶部边框对齐。我想将它(与文本标签一起)垂直居中在按钮上。
'android:gravity' 似乎只适用于文本标签。它不会影响“drawableTop”定位。
我能够使用“android:paddingTop”将其垂直居中 - 但这似乎不是一个好主意。我猜它在不同的屏幕分辨率/尺寸下无法正常工作。
最佳答案
新答案
显然我误解了这个问题。答案是使用:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
style="@android:style/Widget.Button">
...并在里面包含一个 ImageView
和一个 TextView
。
旧答案
这里有一个想法:将您的按钮包装在 FrameLayout 中。像这样的东西:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/ico"
android:foregroundGravity="center">
<Button
android:gravity="center_vertical"
android:id="@+id/StartButton2x2"
android:text="@string/Widget2x2StartBtnLabel"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/widgetbtn_bg" />
</FrameLayout>
顺便说一句,android:layout_width="0dip"
是怎么回事?好像不太对。
关于xml - 如何在 Android 的 Button 上垂直居中 drawableTop 和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2644570/