问题描述
我使用 this 为我的应用构建了一个设置活动/屏幕指导.示例代码如下.能够向此新活动添加工具栏,左侧带有后退箭头,我错过了什么?
I built a Settings activity/screen for my app using this guide. Example code is bellow.What am I missing to be able to add a Toolbar to this new activity with a back arrow in it's left side?
AndroidManifest.xml
....
<activity
android:name=".SettingsActivity"
android:label="Settings"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar"/>
....
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_sync"
android:title="@string/pref_sync"
android:summary="@string/pref_sync_summ"
android:defaultValue="true" />
<ListPreference
android:dependency="pref_sync"
android:key="pref_syncConnectionType"
android:title="@string/pref_syncConnectionType"
android:dialogTitle="@string/pref_syncConnectionType"
android:entries="@array/pref_syncConnectionTypes_entries"
android:entryValues="@array/pref_syncConnectionTypes_values"
android:defaultValue="@string/pref_syncConnectionTypes_default" />
</PreferenceScreen>
SettingsFragment.java
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
SettingsActivity.java
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
推荐答案
您必须在活动布局中包含一个工具栏.例如:
You must include a toolbar in your activity layout. For example:
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.SettingsActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar"/>
</RelativeLayout>
app_bar.xml
app_bar.xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
在您的 Activity.onCreate() 中获取工具栏并设置主页按钮(小箭头):
In your Activity.onCreate() get the toolbar and set a home button (the little arrow):
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
将 SettingsActivity 设置为父 MainActivity 的子项:
Set the SettingsActivity to be a child of parent MainActivity:
<activity
android:name=".activities.SettingsActivity"
android:label="@string/title_activity_main"
android:parentActivityName=".activities.MainActivity" />
这会给你那个小箭头.
另外,这样做:
public class SettingsActivity extends AppCompatActivity
不是这个:
public class SettingsActivity extends Activity
为了使用支持库.
这篇关于使用 PreferenceFragment 构建的设置页面中的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!