【问题标题】:Why button gets bigger when the background is set in android为什么在android中设置背景时按钮变大
【发布时间】:2016-01-27 17:16:17
【问题描述】:

当我使用 xml 中的 android:background 属性为其设置背景时,我遇到了 Button 视图的问题。在我设置背景之前,按钮有它的默认大小。但是当我应用一种颜色作为背景时,它会变大,即使我没有在上面设置不同的widthheight

这是我将背景属性设置为按钮之前的 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>

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

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

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

然后按钮变得比默认大小更大,即使我没有使宽度或高度变大。

这是截图

正如您在上面看到的,取消按钮变大了。实际上我正在设置背景颜色。即使设置了背景颜色,如何修复它以获得默认大小?

【问题讨论】:

    标签: android android-button


    【解决方案1】:

    您必须区分Button 的大小、宽度和高度、您可以点击的整个区域以及用于显示按钮的图像

    android 上的默认按钮对其可绘制对象应用了填充 - 看起来它更小了。它实际上具有相同的大小。

    如果您制作自己的可绘制对象并且不应用填充,则背景将在视图的整个边界内绘制,使其看起来更大。

    您始终可以只使用默认按钮,但使用 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  -->
    

    ...这会让你使用相同的外观。

    【讨论】:

    • @WaiYanHein 是的,由于上述原因。您将 背景 替换为 ColorDrawable,这将填充 整个 视图。您只需使用 backgroundTint 更改颜色,或创建自定义可绘制对象并应用一些填充
    • 对不起。有效。谢谢。它不会破坏现有的 java 代码。对吧?
    • @WaiYanHein 如果您使用 AppCompat,它将正常工作。这就是 app compat 的用途
    • 我也刚刚删除了之前的cmets。
    【解决方案2】:

    在 XML &lt;Button&gt; 标记中使用 android:backgroundTint 属性。

    喜欢:

    android:backgroundTint="#6567dc"

    无需使用&lt;android.support.v7.widget.AppCompatButton&gt; 按钮。

    【讨论】:

    • 我有同样的问题,但我必须将背景设置为具有默认值和 android:itemPressed 项目的 xml 文件,所以我不能使用 backgroundTint。那么还有什么建议吗?
    • 我已经尝试解决您的问题并且我找到了解决方案,它可能看起来很奇怪,但确实有效。在您的 XML 中,在 标记内添加一个 ,其颜色应该是您的应用程序屏幕的背景颜色。像这样:
    【解决方案3】:

    对于希望在代码中(不是在 XML 中)执行此操作的人,您可以使用:

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

    这并没有改变按钮的大小,也适用于旧的 android 版本。 讨论了几种方法here,但是这一种效果很好。

    【讨论】:

      【解决方案4】:

      如果您想支持低于 API 级别 21 的任何内容,另一种选择是创建一个自定义可绘制对象,并在彩色形状周围使用透明笔划。这使您可以灵活地添加块颜色和渐变。

      注意stroke 和透明度会缩小按钮大小

      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
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/button_background"
          android:text="Click Me"/>
      

      【讨论】:

        【解决方案5】:

        这是因为您使用 wrap content 来表示宽度和高度。

        您有多种选择。快速简单的方法?在 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>
        

        【讨论】:

        • 我认为定义宽度和高度是唯一的选择。我设置了填充,不工作。我使用 AppCompatButton ,不工作。
        • 你也可以使用相对布局而不是线性布局
        • 但是当我们不知道按钮内部的文本值时,这个解决方案是没有用的。翻译应用程序时通常是这种情况。所以这个方案并不理想。
        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多