Android 在首选项活动和嵌套首选项上启用SetHomeButton
我的首选项屏幕已扩展Android 在首选项活动和嵌套首选项上启用SetHomeButton,android,android-actionbar,android-4.0-ice-cream-sandwich,preferenceactivity,android-homebutton,Android,Android Actionbar,Android 4.0 Ice Cream Sandwich,Preferenceactivity,Android Homebutton,我的首选项屏幕已扩展首选项活动。对于目标操作系统4.0.3,我想添加,您需要为希望显示向上导航的每个活动启用home按钮和actionbar。您还必须在清单中设置父活动 有关血淋淋的细节,请参见此问题: 您应该通过编写自定义ActionBar样式来添加箭头,以便与应用程序主题一起使用,而不是动态添加箭头。(基本上,请参见)这是我在设置活动中主页按钮的工作代码 @Override public void onCreate(Bundle savedInstanceState)
首选项活动
。对于目标操作系统4.0.3,我想添加,您需要为希望显示向上导航的每个活动启用home按钮和actionbar。您还必须在清单中设置父活动
有关血淋淋的细节,请参见此问题:
您应该通过编写自定义ActionBar样式来添加箭头,以便与应用程序主题一起使用,而不是动态添加箭头。(基本上,请参见)这是我在设置活动中主页按钮的工作代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
root.addView(bar, 0); // insert at top
bar.setTitle("Settings");
bar.setTitleTextColor(Color.WHITE);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
addPreferencesFromResource(R.layout.settings);
}
这是设置_toolbar.xml
<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"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="2dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/gobackleftarrow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
@sapn,谢谢你的回答。但我无法解决我的问题。我无法找到如何添加“我无法使您的解决方案工作”。我得到“没有找到与给定名称匹配的资源(在'theme'处,值为'@android:style/MyTheme')。”。我用谷歌搜索解决了这个问题,但失败了。你已经有了styles.xml吗?如果是,请将中定义的主题添加到该文件中。否,我没有styles.xml.Android将从具有的res/values文件中加载样式。文件的名称(themes.xml或styles.xml)应该无关紧要,但您需要在res/values/中定义一个styles.xml,这样Android就可以加载它们了。我将themes.xml重命名为styles.xml,它就起作用了。但这和我想做的不同。无论如何,谢谢你的帮助。我接受你的回答。
<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"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="2dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/gobackleftarrow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>