我正在开发一个 Material Design 应用程序并且我已经为设置选项声明了一个 xml 文件。我已成功完成此任务,但在运行并从菜单中打开 SettingsActivity 后,它显示如下:
这里的问题是“通知”标题和复选框的颜色很浅,很难看清。
这是SettingsActivity.java
文件的代码:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
这是activity_settings.xml
文件的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxx.abc.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="@id/toolbar"
android:layout_height="match_parent"/>
</RelativeLayout>
这是settings.xml
文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="prefNotification"
android:title="@string/prefNotification">
<CheckBoxPreference
android:id="@+id/prefNotifyCheckBox"
android:dependency="prefNotification"
android:key="prefNotify"
android:summary="@string/pref_notify_summary"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>
由于我是 Android 开发的新手,我无法找出这里的问题。
请告诉我。
提前致谢。
最佳答案
PreferenceFragment
默认使用应用主题的 colorAccent
中存储的值来设置小部件和标题的样式。要解决它,请在 styles.xml
文件中创建一个自定义主题,并将 colorAccent
属性设置为替代颜色:
<style name="theme_name" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/another_color</item>
<item name="colorAccent">@color/yet_another_color</item>
</style>
要将它应用于您的 PreferenceFragment
,请在您的 onCreate()
中调用 getActivity().setTheme(R.style.theme_name)
PreferenceFragment
.
关于java - 为什么 PreferenceCategory 的标题和复选框颜色这么浅?我该如何更改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975457/