android - 操作栏未在首选项 Activity 中显示 :Theme. Appcompat.Light.DarkActionBar

标签 android

我想在我的偏好 Activity 中使用这个主题,因为它有一个漂亮的复选框,但我的操作栏没有显示。

最佳答案

如果您不想在 ActionBarActivity 中使用 PreferenceFragment 并且仍想将现有的 PreferenceActivityAppCompat< 一起使用 支持库 v7,您可以使用 Toolbar 并使用自定义布局覆盖 PreferenceActvity.setContentView()

您可以在保持当前 PreferenceActivity 的同时使用 Toolbar 支持,它适用于 2.3.4 及更高版本

private Toolbar mActionBar;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_MyApp_Settings);

    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.settings);

    mActionBar.setTitle(getTitle());

    //other things to create/init 
}


public void setContentView(int layoutResID) {
    ViewGroup contentView = (ViewGroup) LayoutInflater.from(this).inflate(
        R.layout.settings_activity, new LinearLayout(this), false);

    mActionBar = (Toolbar) contentView.findViewById(R.id.action_bar);
    mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ViewGroup contentWrapper = (ViewGroup) contentView.findViewById(R.id.content_wrapper);
    LayoutInflater.from(this).inflate(layoutResID, contentWrapper, true);

    getWindow().setContentView(contentView);
}

settings_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize" />

    <FrameLayout
        android:id="@+id/content_wrapper"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

样式.xml

<style name="Theme.MyApp.Settings" parent="@style/Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="toolbarStyle">@style/Widget.Toolbar</item>
</style>


<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="navigationIcon">?attr/homeAsUpIndicator</item>
</style>

关于android - 操作栏未在首选项 Activity 中显示 :Theme. Appcompat.Light.DarkActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28933675/

相关文章:

java - 如何使用 Android SIP API 向 SIP 服务器注册 SIP 配置文件?

android - 识别/proc/*/maps 中的代码段的好方法

android - 如何实现 Android Lollipop Curved Motion API?

android - 基于 "Optimization Tips"设置targetSdk

android - 尝试在适配器内添加 fragment

android - 查看通知 channel 是否被用户静音

java - Android Studio 图像 Assets 未正确显示

java - 如果已经连接到另一个 WiFi,则连接到特定 WiFi

android - 在处理时停止 rxJava 可观察链执行

android - Google Glass 在之前 root 过的 XE8 Glass 上安装 XE12