[英]How to have ActionBar in PreferenceActivity?
I have an android app with a UserSettingsActivity
derived from PreferenceActivity
. 我有一个Android应用程序,它具有从PreferenceActivity
派生的UserSettingsActivity
。 When I open this activity, no menu bar is shown. 当我打开此活动时,没有显示菜单栏。
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 . 为了在“设置”屏幕顶部显示“栏”或“操作栏”,我在Google上搜索了该链接和该链接 。 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
( I added just the android.support.v7.widget.Toolbar
part): 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>
and UserSettingsActivity.java
(I added the onPostCreate
part): 和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
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? 这里似乎是什么问题?
Try this. 尝试这个。
Extend your activity with AppCompatActivity 通过AppCompatActivity扩展您的活动
Then add toolbar. 然后添加工具栏。 Refer here: - http://coderzpassion.com/android-working-with-material-design/ 请参阅此处: -http : //coderzpassion.com/android-working-with-material-design/
PreferenceActivity is now deprecated. 现在不推荐使用PreferenceActivity。 Use PreferenceFragment inside any other activity, which can be extended from any parent activity other than PreferenceActivity 在任何其他活动中使用PreferenceFragment,可以从除PreferenceActivity之外的任何父活动中扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.