为什么在Android中设置背景时按钮会变大 [英] Why button gets bigger when the background is set in android

查看:128
本文介绍了为什么在Android中设置背景时按钮会变大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用xml中的android:background属性为其设置背景时,我遇到了Button视图的问题.在设置背景之前,按钮具有其默认大小.但是,当我将颜色用作背景时,即使没有在其上设置其他widthheight,该颜色也会变大.

I encountered a problem with the Button view when I set a background to it using the android:background attribute in xml. Before I set the background, the button has its default size. But when I apply a color as a background, it is getting bigger, even though I did not set a different width or height on it.

这是我将background属性设置为button之前的xml布局

This is my xml layout before I set background attribute to button

<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>

所以取消按钮是默认大小,如屏幕截图所示.

So the cancel button is default size like in screenshot.

但是当我在取消按钮上设置颜色时

But when I set a color on the cancel button like this

 <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" />

然后,即使我没有使宽度或高度变大,按钮也会变得大于默认大小.

Then the button gets bigger than default size, even though I did not make the width or height bigger.

这是屏幕截图

This is the screenshot

如您所见,取消按钮变大了.实际上,我正在设置背景色.设置背景颜色后,如何修复它以获取默认大小?

As you can see above the cancel button became bigger. Actually I am setting background color. How can I fix it to get the default size even after I set the background color?

推荐答案

您必须区分Button的大小,宽度和高度,可以单击的整个区域以及 image 用于显示按钮.

You have to distinguish between the size of the Button, its width and height, the whole area you can click on, and the image used for displaying the button.

android上的默认按钮在其可绘制对象上应用了填充-似乎较小.它实际上具有相同的大小.

The default button on android has a padding applied to its drawable - it seems like it is smaller. It actually has the same size.

如果您创建自己的可绘制对象且不应用填充,则背景将在视图的整个范围内绘制,从而使其看起来更大.

If you make your own drawable and apply no padding, the background will draw within the whole bounds of the view, making it seem bigger.

您始终可以只使用默认按钮,但可以使用AppCompat应用自己的颜色.这支持对默认按钮的背景进行着色.

You can always just use the default button but apply your own color by using AppCompat. This supports tinting the background of the default button.

<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  -->

...这将使您使用相同的外观.

...which will let you use the same appearence.

这篇关于为什么在Android中设置背景时按钮会变大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆