【发布时间】:2016-08-11 10:54:21
【问题描述】:
我正在使用从 NavigationDrawer 启动的 PreferenceFragment。在 preferenceFragment 我显示工具栏。一切看起来都不错,但是当我按工具栏中的箭头返回时,它不起作用。我可以在日志中看到:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
但不要什么都不做。返回的唯一方法是按下设备中的返回按钮
我们将不胜感激。
这是我的代码: 偏好片段:
public class AnPreferenceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pref_with_actionbar);
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new SettingsPreference())
.commit();
}
public static class SettingsPreference extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
}
}
}
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="@string/abc_action_bar_up_description"
app:navigationIcon="?attr/homeAsUpIndicator"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/action_settings" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appbar" />
</RelativeLayout>
【问题讨论】:
-
您是否处理过菜单项中的后按? onOptionMenu 当按下 android.R.id.home 你应该 backPressed()
-
嗨,Amir,不确定我是否理解你的意思。我是android中的新东西。请给我看一下这段代码好吗?
标签: android toolbar android-preferences preferencefragment