如何通过创建一种样式来自定义应用程序中的所有小部件,并通过 AndroidManifest 中的 android:theme 将其应用到应用程序?
最佳答案
这是一个可能有帮助的示例(它可能无法逐字工作,因为我对其进行了调整以简化它,并展示了您可以执行的其他一些操作)。
在 res\values\styles.xml 中:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextColorForTheme">
<item name="android:textColor">@color/red</item>
</style>
</resources>
在 res\values\themes.xml 中:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="myTheme" parent="@style/android:Theme">
<item name="android:listSeparatorTextViewStyle">@style/TextColorForTheme</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowFrame">@null</item>
</style>
</resources>
然后在 AndroidManifest.xml 中,设置整个应用程序或单个 Activity 以使用该主题:
<application
android:theme="@style/myTheme"
<snip>
<activity
android:theme="@style/myTheme"
<snip>
或者,您可以在 Java Activity 的代码中设置主题:
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setTheme(R.style.myTheme);
<snip>
关于Android:使用一个主题自定义所有 UI 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829101/