繁体   English   中英

如何在PreferenceActivity中使用ActionBar?

[英]How to have ActionBar in PreferenceActivity?

我有一个Android应用程序,它具有从PreferenceActivity派生的UserSettingsActivity 当我打开此活动时,没有显示菜单栏。

为了在“设置”屏幕顶部显示“栏”或“操作栏”,我在Google上搜索了该链接该链接 因为第一个答案似乎已经过时,所以我尝试了第二个不起作用的链接中的建议。 因此,我再次发布此问题。.这是我正在使用的代码片段:

preferences.xml (我仅添加了android.support.v7.widget.Toolbar部分):

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

    <ListPreference
        android:title="@string/UpdateInterval"
        android:summary="@string/UpdateIntervalText"
        android:key="updateInterval"
        android:defaultValue="24"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />

    <ListPreference
        android:title="@string/LimitSetting"
        android:summary="@string/LimitSettingText"
        android:key="limitSetting"
        android:entries="@array/limitArray"
        android:entryValues="@array/limitValues" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

UserSettingsActivity.java (我添加了onPostCreate部分):

public class UserSettingActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.xml.preferences, root, false);
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences); // <<-- ERROR HERE
            // doing something here

        }
    }

}

但是,出现以下错误:

Caused by: java.lang.ClassCastException: android.support.v7.widget.Toolbar cannot be cast to android.preference.Preference

在标记的线上。 但是,在这两个文件中没有添加片段的情况下,该应用程序确实可以正常运行,但是没有(动作)栏。

这里似乎是什么问题?

尝试这个。
通过AppCompatActivity扩展您的活动
然后添加工具栏。 请参阅此处: -http : //coderzpassion.com/android-working-with-material-design/

现在不推荐使用PreferenceActivity。 在任何其他活动中使用PreferenceFragment,可以从除PreferenceActivity之外的任何父活动中扩展

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM