Wai*_*ein 17 android android-button
Button当我使用android:backgroundxml中的属性为其设置背景时,我遇到了视图问题.在设置背景之前,该按钮具有默认大小.但是,当我将颜色作为背景,这是挺大的,虽然我没有设置不同的width或者height在其上.
在将background属性设置为button之前,这是我的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_row_option_done"
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Edit"
android:id="@+id/btn_row_option_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Delete"
android:id="@+id/btn_row_option_delete"
android:background="@color/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
所以取消按钮是默认大小,如截图.
但是当我在这样的取消按钮上设置颜色时
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
然后按钮变得比默认大小更大,即使我没有使宽度或高度更大.
这是截图
如上所示,取消按钮变大了.其实我正在设置背景颜色.即使在设置背景颜色后,如何修复它以获得默认大小?
Dav*_*jak 17
您必须区分尺寸Button,宽度和高度,可以单击的整个区域以及用于显示按钮的图像.
android上的默认按钮有一个应用于其drawable的填充 - 看起来它更小.它实际上具有相同的大小.
如果您自己绘制并且不应用填充,则背景将在视图的整个边界内绘制,使其看起来更大.
您始终只需使用默认按钮,但可以使用AppCompat应用自己的颜色.这支持着色默认按钮的背景.
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#ffaa00"/> <!-- <--- Your color here -->
Run Code Online (Sandbox Code Playgroud)
...这将让你使用相同的外观.
android:backgroundTint在 XML<Button>标记中使用属性。
喜欢:
android:backgroundTint="#6567dc"
无需使用<android.support.v7.widget.AppCompatButton>按钮。
| 归档时间: |
|
| 查看次数: |
6237 次 |
| 最近记录: |