Как добавить переключатель внутри панели инструментов в android? - PullRequest
0 голосов
/ 30 января 2020

Я занимаюсь разработкой android приложения и хочу добавить switch внутри toolbar оно не отображается

ниже мой xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Switch
            android:id="@+id/switch1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Switch" />

    </androidx.appcompat.widget.Toolbar>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

следующий экран текущий UI

current ui

Я хочу знать, как добиться такого рода UI, что я должен сделать

Ответы [ 2 ]

1 голос
/ 30 января 2020

Просто измените AppTheme на NoActionBar из styles.xml

стилей. xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Теперь запустите ваше приложение, переключатель будет отображаться в верхней части панели инструментов.

0 голосов
/ 30 января 2020

Вам необходимо использовать пользовательскую панель инструментов, которую вы уже используете в своем файле xml, но вам нужно удалить панель инструментов по умолчанию, вызвав getSupportActionBar().hide(); в действии onCreate (). и установите цвет панели инструментов в xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<androidx.appcompat.widget.Toolbar
    android:elevation="@dimen/margin_large"
    android:background="@color/colorPrimaryDark"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Switch
        android:id="@+id/switch1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Switch" />

</androidx.appcompat.widget.Toolbar>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

...