问题描述
我有一个Android应用程序一个 UserSettingsActivity
从 preferenceActivity
导出。当我打开这个活动,没有显示菜单栏。
I have an android app with a UserSettingsActivity
derived from PreferenceActivity
. When I open this activity, no menu bar is shown.
为了有酒吧或操作栏上的设置屏幕的上方,我搜索了它在谷歌上找到的和<一个href=\"http://stackoverflow.com/questions/26509180/no-actionbar-in-$p$pferenceactivity-after-upgrade-to-support-library-v21\">this链接。因为第一次的答案似乎已经过时,我想建议在第二环节的的不工作的。因此,我再次发布了这个问题。这里是code的作品我使用:
In order to have the 'bar' or 'action bar' on top of the settings screen, I searched for it in google on found this link and this link. Because the first answer seems outdated, I tried the suggestion in the second link which does not work. Therefore I am posting this question again.. Here are the pieces of code I am using:
preferences.xml
(我刚刚加入了 android.support.v7.widget.Toolbar
部分):
preferences.xml
( I added just the android.support.v7.widget.Toolbar
part):
<?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
}
}
}
不过,我得到以下错误:
However, I get the following error:
Caused by: java.lang.ClassCastException: android.support.v7.widget.Toolbar cannot be cast to android.preference.Preference
在标记线。如果没有这两个文件的附加件的应用程序没有工作,没有(动作)-bar,但是...
on the marked line. Without the added pieces in those two files the app did work, without the (action)-bar, however...
什么似乎是这里的问题?
What seems to be the problem here?
推荐答案
preferenceActivity现在是很precated。使用preferenceFragment任何其他活动,这可以从超过preferenceActivity
PreferenceActivity is now deprecated. Use PreferenceFragment inside any other activity, which can be extended from any parent activity other than PreferenceActivity
这篇关于如何有动作条在preferenceActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!