我找到了教学主题 - 如何为整个应用设置主题:
Setting global styles for Views in Android
它根本不适用于我。这是我的代码:
style.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--1-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:radioButtonStyle">@style/TBRadioButtonStyle</item>
</style>
<style name="TBRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/state_color_selector</item>
</style>
</resources>
state_color_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<color android:color="#F00" />
</item>
<item android:state_checked="false">
<color android:color="#0F0" />
</item>
</selector>
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.app" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".controller.ApplicationController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".view.DrawersWidget"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
创建RadioButton的.java文件:
RadioButton newButton = new RadioButton(ApplicationController.INSTANCE.getApplicationContext());
//...
newButton.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
newButton.setButtonDrawable(new BitmapDrawable(ApplicationController.INSTANCE.getResources(), filteredBitmap));
newButton.setText(someText);
答案 0 :(得分:1)
创建RadioButton时,请使用Activity的Context,而不是ApplicationContext。