Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使setBackgroundColor在一个按钮中的所有android版本上工作?_Android_Android Studio_Android Button_Material Components Android - Fatal编程技术网

如何使setBackgroundColor在一个按钮中的所有android版本上工作?

如何使setBackgroundColor在一个按钮中的所有android版本上工作?,android,android-studio,android-button,material-components-android,Android,Android Studio,Android Button,Material Components Android,我想在ClickListener中的按钮上设置BackgroundColor。我成功地做到了这一点,但我注意到它只适用于某些android版本。我用安卓4.4的手机测试了它,它可以工作,但在安卓9的版本下,它不能工作。以下是我的代码和build.gradle文件内容: Button usd=findViewById(R.id.button_divide); Button cdf=findViewById(R.id.button_multiply); usd.setOnClick

我想在ClickListener中的按钮上设置BackgroundColor。我成功地做到了这一点,但我注意到它只适用于某些android版本。我用安卓4.4的手机测试了它,它可以工作,但在安卓9的版本下,它不能工作。以下是我的代码和build.gradle文件内容:

 Button usd=findViewById(R.id.button_divide);
    Button cdf=findViewById(R.id.button_multiply);
    usd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor=sharedPref.edit();
            editor.putString("currency","USD");
            editor.apply();
            usd.setBackgroundColor(Color.parseColor("#515de1"));
            usd.setTextColor(Color.WHITE);
            cdf.setBackgroundColor(Color.WHITE);
            cdf.setTextColor(Color.parseColor("#515de1"));
        }

    });
此代码扩展并支持活动 和我的build.gradle文件:

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.example.e_mpatanfc"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
以下是我的主题:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

@颜色/紫色500
@颜色/紫色\u 700
@颜色/白色
@颜色/青色200
@颜色/青绿色700
@颜色/黑色
?attr/colorPrimaryVariant
还有夜间版本

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

@颜色/紫色200
@颜色/紫色\u 700
@颜色/黑色
@颜色/青色200
@颜色/青色200
@颜色/黑色
?attr/colorPrimaryVariant
最后是values/style.xml内容:

   <?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="BottomNavigationView">
     <item name="android:textSize">24sp</item>

  </style>
   <style name="BottomNavigationView.Active">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">#515de1</item>
 </style>
</resources>

24便士
24便士
#515de1

使用setBackgroundResource而不是像这样使用setBackgroundColor

setBackgroundResource(R.color.red)


并在colors.xml文件中定义颜色

您可以使用方法
setBackgroundTintList

usd.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));
cdf.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));

使用
主题.MaterialComponents.*
主题,您的按钮将在运行时替换为
MaterialButton
。此方法适用于minsdk>14。

发布你的应用程序。我这样做了,但不起作用。如果使用setBackgroundResource而不是setBackgroundColor,你能告诉我输出是什么吗。。同时发布你的styles.xml文件两种情况下的结果相同,文本颜色设置为白色,但背景保持不变。android:colorAccent在styles.xml中的主题中,你的意思是我在主题和样式文件中添加了colorAccent吗?我尝试了你的解决方案,但它需要API级别21,所以我在if语句中添加了它,当setbackgroundColor在我的android 4.4(api小于21)上运行时,我使用了你的解决方案和我的解决方案,以确保它在每个应用程序上都能运行smartphone@RamsesKouam将您的按钮铸造为MaterialButton。使用材质组件主题,您的按钮将在运行时替换为MaterialButton。材质组件库的minsdk为14。这段代码也适用于android 4.4