java 在 android 嵌套的 PreferenceScreen 上显示向上/后退按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14190713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:25:17  来源:igfitidea点击:

Show up/back button on android nested PreferenceScreen?

javaandroidandroid-actionbar

提问by lisovaccaro

I have a two level PreferenceScreen:

我有一个两级 PreferenceScreen:

<PreferenceScreen>
general settings
   <PreferenceScreen android:key="adv_settings">
   more advanced settings
   </PreferenceScreen>
</PreferenceScreen>

My problem is that the second screen doesn't show the back/up button on the action bar automatically. How do I make the up button appear on adv_settings?

我的问题是第二个屏幕不会自动在操作栏上显示后退/向上按钮。如何使向上按钮出现在 adv_settings 上?

回答by liucheia

You can add the arrow by writing a custom ActionBar style to be used with your application theme.

您可以通过编写要与应用程序主题一起使用的自定义 ActionBar 样式来添加箭头。

res/values-v11/styles.xml:(or add these to your existing styles.xml)

res/values-v11/styles.xml:(或将这些添加到现有的styles.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>    
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
  </style>

  <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
  </style>
</resources>

Then apply this theme in your AndroidManifest.xml:

然后在你的 AndroidManifest.xml 中应用这个主题:

<application android:theme="@style/MyTheme">



Note: The obvious way to add this arrow should be to call:

注意:添加此箭头的明显方法应该是调用:

getActionBar().setDisplayHomeAsUpEnabled(true);

once the second screen has loaded, but I think there's an Android bug where getActionBar() always returns the first-tier ActionBar object, as opposed to the one that is currently visible, so setting the arrow dynamically fails.

加载第二个屏幕后,但我认为存在一个 Android 错误,其中 getActionBar() 始终返回第一层 ActionBar 对象,而不是当前可见的对象,因此动态设置箭头会失败。

回答by ThanksMister

This may be more work, but you could create two PreferenceAtivity files each with their own PreferenceFragment. Each PreferenceFragment will have its own PreferenceScreen XML (the first level screen, and the second. level screen). From the firsts level screen you launch the second PreferenceActivity with an Intent within the tag. In the second PreferenceActivity you can set the home icon by doing this:

这可能需要更多的工作,但您可以创建两个 PreferenceAtivity 文件,每个文件都有自己的 PreferenceFragment。每个 PreferenceFragment 都有自己的 PreferenceScreen XML(第一级屏幕和第二级屏幕)。从第一级屏幕启动第二个 PreferenceActivity,并在标签中包含一个 Intent。在第二个 PreferenceActivity 中,您可以通过执行以下操作来设置主页图标:

ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);

and then also had a handler for the home button:

然后还有一个主页按钮的处理程序:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (item.getItemId() == android.R.id.home) {
        finish();
    }

    return false;
}

Assets:

资产:

FirstPreferenceActivty
FirstPreferenceFragment
pref_first.xml (layout with PreferenceScreen and Prefernce nodes)

SecondPreferenceActivty
SecondPreferenceFragment
pref_second.xml (layout with PreferenceScreen and Prefernce nodes)

回答by mico

If the button does not come automatically, you could add it manually like done on these links:

如果按钮没有自动出现,您可以像在这些链接上所做的那样手动添加它:

Android: How can make custom PreferenceScreen?

Android:如何制作自定义 PreferenceScreen?

http://pastebin.com/334Eip35

http://pastebin.com/334Eip35

There is a example code in second answer of that SO question and the snippet in it is taken probably from the another pastebin location.

该 SO 问题的第二个答案中有一个示例代码,其中的片段可能取自另一个 pastebin 位置。