[英]Why button gets bigger when the background is set in android
当我使用 xml 中的android:background
属性为其设置背景时,我遇到了Button
视图的问题。 在设置背景之前,按钮具有默认大小。 但是当我应用一种颜色作为背景时,它变得越来越大,即使我没有设置不同的width
或height
。
这是我将背景属性设置为按钮之前的 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" />
然后按钮变得比默认大小更大,即使我没有使宽度或高度更大。
这是截图
如您所见,取消按钮变大了。 其实我正在设置背景颜色。 即使设置了背景颜色,如何修复它以获得默认大小?
您必须区分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 -->
...这将让您使用相同的外观。
在 XML <Button>
标签中使用android:backgroundTint
属性。
喜欢:
android:backgroundTint="#6567dc"
无需使用<android.support.v7.widget.AppCompatButton>
按钮。
对于希望在代码中执行此操作的人(不在 XML 中),您可以使用以下命令:
button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
这并没有改变按钮的大小,并且也适用于旧的 android 版本。 这里讨论了几种方法,但这个方法很完美。
这是因为您使用了宽度和高度的换行内容。
您有多种选择。 快速简单的方法? 设置按钮的值,指定 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>
如果您想支持 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"/>
尝试使用此属性设置按钮颜色
button.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.