【问题标题】:Change button background color and save older background更改按钮背景颜色并保存旧背景
【发布时间】:2018-05-29 14:57:21
【问题描述】:

我需要将按钮背景颜色更改为红色。

我试试这个button.SetBackgroundColor(Color.Red);,但按钮变大了,然后我试试这个button.BackgroundTintList = (ColorStateList.ValueOf(Color.Red));,效果很好。

但在那之后我需要像以前一样放置背景按钮,但我不能这样做。我有另一个具有相同背景的按钮,并尝试使用它从那里复制 Button anotherButton = _v.FindViewById<Button>(Resource.Id.bt_anotherButton); button.Background = anotherButton.Background;

但不是把另一个按钮背景等于按钮背景,而是相反,两个按钮都变成了红色。

有人可以帮忙吗?

【问题讨论】:

  • 可以保存以前的颜色。或者您可以使用 Selector drawable 来绘制多个状态。
  • 我正在尝试保存以前的背景,但我做不到。我有这个Drawable dr = button.Background; 然后把博士作为免费但保持红色

标签: android xamarin


【解决方案1】:

就像@hichame 所说,您的按钮没有变大。您可以阅读thisthis 来了解为什么您的按钮看起来更大。

关于按钮的颜色,可以参考this使用android.R.drawable.btn_default

button.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault);

重置您的按钮。但是,结果不像原点按钮。


最后,我得到了默认按钮的颜色:#D6D7D7

您可以使用此值将按钮的颜色重置为默认值:

button.Background.SetTintList(ColorStateList.ValueOf(Color.ParseColor("#D6D7D7")));

更新:

看完this的回答,我找到了解决办法,你需要使用AppCompatButton,安装Xamarin.Android.Support.v7.AppCompat,在你的布局中使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  <android.support.v7.widget.AppCompatButton
      android:id="@+id/bt1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
  <android.support.v7.widget.AppCompatButton
      android:id="@+id/bt2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
</LinearLayout>

在您的MainActivity 中使用SupportBackgroundTintList(这也是您的返回值为空的原因):

public class MainActivity : AppCompatActivity,View.IOnClickListener
{
    AppCompatButton bt1;
    ColorStateList backgroundTintList;
    public void OnClick(View v)
    {
        bt1.SupportBackgroundTintList=(backgroundTintList);
        //bt1.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault);
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        
        bt1 = FindViewById<AppCompatButton>(Resource.Id.bt1);
        AppCompatButton bt2 = FindViewById<AppCompatButton>(Resource.Id.bt2);
       
        backgroundTintList = bt2.SupportBackgroundTintList;

        bt1.SupportBackgroundTintList=ColorStateList.ValueOf(Color.Red);

        bt2.SetOnClickListener(this); 
       
    }
}

结果:

【讨论】:

  • 感谢您的回答。我试试这个button.Background.SetTintList(ColorStateList.ValueOf(Color.ParseColor("#D6D7D7")));,它可以工作。现在我有了这个code 并且效果很好。
【解决方案2】:

您的按钮没有变大,但正在获得默认 Android 按钮的填充。 您必须将色调设置为不修改背景设置的大小,您可以通过以下方式进行:

var previousTint = button.BackgroundTintList;
button.Background.SetTint (Color.HoloRedDark);

完成后,将之前的色调调回原位。

【讨论】:

  • 感谢您的回答。这个 var previousTint = button.BackgroundTintList;返回空值。现在我有这个:pastebin.com/du3jVfuL
猜你喜欢
  • 2015-06-04
  • 1970-01-01
  • 2021-06-22
  • 2015-04-03
  • 2011-06-26
  • 2021-07-05
相关资源
最近更新 更多