setHomeButtonEnabled on PreferenceActivity and nested preference

给你一囗甜甜゛ 提交于 2019-12-06 00:30:35

问题


I have preference screen extended PreferenceActivity. For targeting OS 4.0.3, I wanted to add < icon on action bar so I did this in onCreate().

ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);

It worked. < was added to left of app icon. But when I tap the item which goes into the next level (more detail screen), the < won't be displayed. Returning to the top level, the < appears again.

I have never thought about a mechanism of nested preference since smart the PreferenceActivity hides it. Now my question is, why won't PreferenceActivity display the < on nested preference?

I don't want to argue that I don't need to add < to the preference screen. (Even some of Google's app add, some don't, so I think there is no solid rule for this.)

If there is a simple solution for this, I want to solve this issue.


回答1:


Instead of dynamically adding this, you should add the arrow by writing a custom ActionBar style to be used with your application theme. (Basically, see https://stackoverflow.com/a/16247111/582004)




回答2:


This is my working code for Home button on Settings activity

    @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);

}

This is the 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>



回答3:


You need to enable the home button and actionbar for each Activity that you wish to show the UP navigation. You must also set the parent activity in the manifest.

See this question for the gory details: Up indicator does not show up



来源:https://stackoverflow.com/questions/11971800/sethomebuttonenabled-on-preferenceactivity-and-nested-preference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!