简体   繁体   English

为什么在android中设置背景时按钮会变大

[英]Why button gets bigger when the background is set in android

I encountered a problem with the Button view when I set a background to it using the android:background attribute in xml.当我使用 xml 中的android:background属性为其设置背景时,我遇到了Button视图的问题。 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.但是当我应用一种颜色作为背景时,它变得越来越大,即使我没有设置不同的widthheight

This is my xml layout before I set background attribute to 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>

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?即使设置了背景颜色,如何修复它以获得默认大小?

在此处输入图片说明

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.您必须区分Button的大小、其宽度和高度、您可以单击的整个区域以及用于显示按钮的图像

The default button on android has a padding applied to its drawable - it seems like it is smaller. android 上的默认按钮对其可绘制对象应用了填充 - 看起来它更小。 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.如果您制作自己的可绘制对象并且不应用填充,背景将在视图的整个边界内绘制,使其看起来更大。

You can always just use the default button but apply your own color by using AppCompat.您始终可以只使用默认按钮,但使用 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. ...这将让您使用相同的外观。

Use android:backgroundTint attribute in the XML <Button> tag.在 XML <Button>标签中使用android:backgroundTint属性。

Like:喜欢:

android:backgroundTint="#6567dc"

No need to use <android.support.v7.widget.AppCompatButton> Button.无需使用<android.support.v7.widget.AppCompatButton>按钮。

For people looking for doing it in the code (Not in XML), you can use this:对于希望在代码中执行此操作的人(不在 XML 中),您可以使用以下命令:

button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);

this didn't change the size of the button and works with the old android versions too.这并没有改变按钮的大小,并且也适用于旧的 android 版本。 Several methods discussed here , but this one worked perfect. 这里讨论了几种方法,但这个方法很完美。

It's because you are using wrap content for widht and height.这是因为您使用了宽度和高度的换行内容。

You have several options.您有多种选择。 Quick and simple way?快速简单的方法? Set the value of the buttons specifying a size in DP like:设置按钮的值,指定 DP 中的大小,例如:

<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="200dp"
        android:layout_height="200dp" />

    <Button
        android:background="@color/white"
        android:layout_gravity="center_horizontal"
        android:text="Edit"
        android:id="@+id/btn_row_option_edit"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Delete"
        android:id="@+id/btn_row_option_delete"
        android:background="@color/red"
        android:layout_width="200dp"
        android:layout_height="200dp" />

    <Button
        android:layout_gravity="center_horizontal"
        android:text="Cancel"
        android:id="@+id/btn_row_option_cancel"
        android:layout_width="100dp"
        android:layout_height="100dp" />
</LinearLayout>

Another option if you want to support anything below API Level 21 is to create a custom drawable with a transparent stroke around a colored shape.如果您想支持 API 级别 21 以下的任何内容,另一种选择是创建一个自定义可绘制对象,并在彩色形状周围使用透明笔触。 This gives you the flexibility of also adding block colors and gradients.这使您还可以灵活地添加块颜色和渐变。

Note the stroke with and transparency which shrinks the button size注意缩小按钮大小的stroke和透明度

button_background.xml button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="5dp" />
    <padding
        android:left="10dp"
        android:right="10dp"
        android:top="0dp"
        android:bottom="0dp"/>

    <!-- This is where the magic happens -->
    <stroke android:color="#00FF0000" android:width="10dp"/>

    <solid android:color="#000000" />
</shape>

Button按钮

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"
    android:text="Click Me"/>

尝试使用此属性设置按钮颜色

button.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM