setBackgroundColor 改变按钮的颜色
setBackgroundColor changes color of more then the button
我正在尝试为按钮设置颜色,但是当我写:
button.setBackgroundColor(getResources().getColor(R.color.white));
按钮变成白色,但周围也有一些 space(我在 linearLayout
中有几个按钮,所以它看起来像一个大的白色按钮)。
有人知道如何解决这个问题吗?
更新:我的XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="@android:color/white"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</LinearLayout>
这里左边的按钮看起来比右边的大,因为我改变了它的颜色
您不需要在 activity
中编写该代码
就在你的 XML :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="#ffffff"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
访问此网站获取颜色代码:
这是因为按钮的默认实现使用自定义可绘制对象作为背景,更改背景会覆盖它并丢失所有样式。
您要做的是用一种颜色覆盖现有的可绘制背景:
button.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
您还可以找到按钮使用的默认样式,复制它并在那里更改颜色,但这样会更麻烦。
你试过了吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2"
>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="@android:color/white"
android:id="@+id/button1"
/>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</LinearLayout>
在您的父布局 (LinearLayout
) 中,您将 weightSum
设置为 2,但不会通过在子元素中使用 android:layout_weight="1"
为子元素做出贡献
将按钮的高度和宽度设置为特定大小,以便在使用背景色时不包裹内容。
You can try in Xamarin
button.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#f00ece"), Android.Graphics.PorterDuff.Mode.Multiply);
我正在尝试为按钮设置颜色,但是当我写:
button.setBackgroundColor(getResources().getColor(R.color.white));
按钮变成白色,但周围也有一些 space(我在 linearLayout
中有几个按钮,所以它看起来像一个大的白色按钮)。
有人知道如何解决这个问题吗?
更新:我的XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="@android:color/white"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</LinearLayout>
这里左边的按钮看起来比右边的大,因为我改变了它的颜色
您不需要在 activity
中编写该代码就在你的 XML :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="#ffffff"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
访问此网站获取颜色代码:
这是因为按钮的默认实现使用自定义可绘制对象作为背景,更改背景会覆盖它并丢失所有样式。
您要做的是用一种颜色覆盖现有的可绘制背景:
button.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
您还可以找到按钮使用的默认样式,复制它并在那里更改颜色,但这样会更麻烦。
你试过了吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2"
>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:background="@android:color/white"
android:id="@+id/button1"
/>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</LinearLayout>
在您的父布局 (LinearLayout
) 中,您将 weightSum
设置为 2,但不会通过在子元素中使用 android:layout_weight="1"
为子元素做出贡献
将按钮的高度和宽度设置为特定大小,以便在使用背景色时不包裹内容。
You can try in Xamarin
button.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#f00ece"), Android.Graphics.PorterDuff.Mode.Multiply);