android - 如何使 setBackgroundColor 在按钮中的所有 android 版本上工作?
问题描述
我想在 ClickListener 中的按钮上设置背景颜色。我成功了,但我注意到它只适用于某些 android 版本。我用安卓 4.4 的手机对其进行了测试,它可以工作,但使用 android 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"));
}
});
此代码扩展了AppCompatActivity 和我的 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>
和夜间版
<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>
最后是 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>
解决方案
您可以使用该方法setBackgroundTintList
:
usd.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));
cdf.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));
使用Theme.MaterialComponents.*
主题,您的 Button 在运行时被替换为MaterialButton
. 此方法适用于 minsdk>14。
推荐阅读
- python - 为什么在运行以下代码(python、Kivy)时会出现“TypeError”?
- rdf - Why fuseki web UI does not show my dataset?
- java - 正则表达式,用于在开始时不允许空格和值、A、AR 或 Ar
- c# - C# POS - 'Microsoft.PointOfService.Management.Explorer' 的类型初始化程序引发异常
- azure - Azure 映射 REST API 链接不起作用
- php - 可能有错误的 PHP 安装 - 信息页空白 - LAMP
- c++ - 如何通过 Valgrind 查找 QThreads 程序中的内存泄漏在哪里?
- lora - 路由键逻辑
- android - Kotlin 设置文本方法不起作用
- python - 如何在 git 中测试前/后提交脚本?