Добавить панель инструментов / панель действий в PreferenceActivity - PullRequest
0 голосов
/ 24 мая 2018

Я пытаюсь добавить панель инструментов к своему PreferenceActivity.Я осмотрел здесь и несколько мест, и, кажется, ничего не работает.

Я использую заголовки для моего верхнего уровня.Затем я попытался использовать свой собственный макет в onCreate, включая нужную панель инструментов и убедившись, что он имеет вид с именем «@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>

Это работало нормально для верхнего уровняно затем на следующем уровне не удалось найти имя представления «android: id / prefs».

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

Единственный код в onCreate фрагмента - это AddPreferencesFromResource.

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

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

Я пытался использовать тему и указывал в манифесте, но безрезультатно.

1 Ответ

0 голосов
/ 24 мая 2018

Вы можете использовать PreferenceFragmentCompat, завернутый в обычный AppCompatActivity.Затем вы можете легко прикрепить панель приложения к своей деятельности, используя

setContentView(R.layout.activity_setting);

, а настройки можно установить для фрагмента, используя

setPreferencesFromResource(R.xml.preferences_button_interaction, rootKey);

Если вы хотите добавить панель инструментов к своей текущей деятельности, выможно попробовать этот метод

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