简体   繁体   中英

Add Toolbar/Actionbar to PreferenceActivity

I am trying to add a toolbar to my PreferenceActivity . I have looked around here and a number of places and nothing seems to work.

I am using headers for my top-level. I tried to then use my own layout in onCreate including the required toolbar and making sure it has a view by the name of "@android:id/list".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:title="@string/app_name"
            app:navigationIcon="@drawable/back"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>


</LinearLayout>

This worked okay for the top level but then the next level failed looking for a view name "android:id/prefs".

java.lang.IllegalArgumentException: No view found for id 0x102040a (android:id/prefs) for fragment PreferencesFragmentButtonInteraction{7a6584a #0 id=0x102040a}

The only code in the onCreate of the fragment is the AddPreferencesFromResource.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences_button_interaction);
}

I have tried to use a theme and set in in the manifest but to no avail.

You can use PreferenceFragmentCompat wrapped in a normal AppCompatActivity. Then you can easily attach an appbar to your activity using

setContentView(R.layout.activity_setting);

and preferences can be set to the fragment using

setPreferencesFromResource(R.xml.preferences_button_interaction, rootKey);

If you wish to add toolbar to your current activity, you can try this method

private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat

if (rootView != null) {
    View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
    rootView.addView(view, 0);

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    // Show the Up button in the action bar.
    actionBar.setDisplayHomeAsUpEnabled(true);
}
}

app_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<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"/>

</android.support.design.widget.AppBarLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM