setHomeButtonEnabled on PreferenceActivity and nested preference

亡梦爱人 提交于 2019-12-04 07:50:59
liucheia

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)

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>
span

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

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