В настоящее время я пытаюсь реализовать NavigationDrawer в своем приложении и следую руководству разработчика Android относительно того, как это реализовано.

Однако при попытке установить пользовательский Toolbar в качестве ActionBar Toolbar не отображается в приложении, как будто метод setSupprtActionBar() ничего не делает.

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

РЕДАКТИРОВАТЬ: Следуя предложениям ʍѳђ ઽ ૯ ท, вот обновленный код, который по-прежнему не отображает панель инструментов.

Вот мой MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Sets custom toolbar as default toolbar and imports menu icon
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Creates navigation drawer
        mDrawerLayout = findViewById(R.id.drawer_layout);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        // set item as selected to persist highlight
                        menuItem.setChecked(true);
                        // close drawer when item is tapped
                        mDrawerLayout.closeDrawers();

                        // Add code here to update the UI based on the item selected
                        // For example, swap UI fragments here

                        return true;
                    }
                });
    }

Вот мой activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            android:elevation="4dp" />

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

            <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorBackground">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="12dp">

                    <android.support.v7.widget.CardView
                        android:id="@+id/weather_mode_card"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="14dp">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#fff"
                            android:elevation="4dp"
                            android:padding="10dp">

                            <ImageView
                                android:id="@+id/weather_mode_icon"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_marginTop="6dp"
                                android:layout_marginEnd="10dp"
                                android:src="@drawable/icon_weather" />

                            <TextView
                                android:id="@+id/weather_mode_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_toEndOf="@+id/weather_mode_icon"
                                android:paddingBottom="5dp"
                                android:text="@string/weather_mode"
                                android:textSize="20sp" />

                            <TextView
                                android:id="@+id/weather_mode_desc"
                                android:layout_width="220dp"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/weather_mode_title"
                                android:layout_toEndOf="@+id/weather_mode_icon"
                                android:layout_toStartOf="@id/weather_mode_button"
                                android:text="@string/weather_mode_desc"
                                android:textColor="#8b8b8b"
                                android:textSize="14sp" />

                            <Button
                                android:id="@+id/weather_mode_button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:layout_centerVertical="true"
                                android:text="@string/mode_button"/>

                        </RelativeLayout>

                    </android.support.v7.widget.CardView>

                    <android.support.v7.widget.CardView
                        android:id="@+id/wifi_mode_card"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="14dp">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#fff"
                            android:elevation="4dp"
                            android:padding="10dp">

                            <ImageView
                                android:id="@+id/wifi_mode_icon"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="6dp"
                                android:layout_alignParentTop="true"
                                android:layout_marginEnd="10dp"
                                android:src="@drawable/icon_wifi" />

                            <TextView
                                android:id="@+id/wifi_mode_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_toEndOf="@+id/wifi_mode_icon"
                                android:paddingBottom="5dp"
                                android:text="@string/wifi_mode"
                                android:textSize="20sp" />

                            <TextView
                                android:id="@+id/wifi_mode_desc"
                                android:layout_width="220dp"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/wifi_mode_title"
                                android:layout_toEndOf="@+id/wifi_mode_icon"
                                android:layout_toStartOf="@id/wifi_mode_button"
                                android:text="@string/wifi_mode_desc"
                                android:textColor="#8b8b8b"
                                android:textSize="14sp" />

                            <Button
                                android:id="@+id/wifi_mode_button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:layout_centerVertical="true"
                                android:text="@string/mode_button"/>

                        </RelativeLayout>

                    </android.support.v7.widget.CardView>

                    <android.support.v7.widget.CardView
                        android:id="@+id/temp_mode_card"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="14dp">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#fff"
                            android:elevation="4dp"
                            android:padding="10dp">

                            <ImageView
                                android:id="@+id/temp_mode_icon"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="6dp"
                                android:layout_alignParentTop="true"
                                android:layout_marginEnd="10dp"
                                android:src="@drawable/icon_temp" />

                            <TextView
                                android:id="@+id/temp_mode_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_toEndOf="@+id/temp_mode_icon"
                                android:paddingBottom="5dp"
                                android:text="@string/temp_mode"
                                android:textSize="20sp" />

                            <TextView
                                android:id="@+id/temp_mode_desc"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/temp_mode_title"
                                android:layout_toEndOf="@+id/temp_mode_icon"
                                android:layout_toStartOf="@id/temp_mode_button"
                                android:text="@string/temp_mode_desc"
                                android:textColor="#8b8b8b"
                                android:textSize="14sp" />

                            <Button
                                android:id="@+id/temp_mode_button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:layout_centerVertical="true"
                                android:text="@string/mode_button"/>

                        </RelativeLayout>

                    </android.support.v7.widget.CardView>

                </LinearLayout>

            </ScrollView>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header" />

</android.support.v4.widget.DrawerLayout>

Вот мой styles.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>
        <item name="android:colorForeground">@color/foreground_material_light</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


</resources>

А вот и мой AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
1
B. Stein 16 Сен 2018 в 15:18

2 ответа

Лучший ответ

Причина, по которой я предполагаю, это FrameLayout под Toolbar и фактически весь макет. Я прокомментировал важные вещи о дизайне внутри блока комментариев в кодах макета.

В вашем styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>  
     ...
    </style>

В вашем AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true">
    <activity 
              android:name=".MainActivity"
              android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

В вашем макете :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:title="MyToolbar" />

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


        <!--<FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />--> <!-- This shouldn't be here maybe under the NestedScrollView -->

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/red"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <!-- Here your content -->


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="12dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/weather_mode_card"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="14dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:elevation="4dp"
                        android:padding="10dp">

                        <ImageView
                            android:id="@+id/weather_mode_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_marginEnd="10dp"
                            android:layout_marginTop="6dp" />

                        <TextView
                            android:id="@+id/weather_mode_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_toEndOf="@+id/weather_mode_icon"
                            android:paddingBottom="5dp"
                            android:text="weather_mode"
                            android:textSize="20sp" />

                        <TextView
                            android:id="@+id/weather_mode_desc"
                            android:layout_width="220dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/weather_mode_title"
                            android:layout_toEndOf="@+id/weather_mode_icon"
                            android:layout_toStartOf="@id/weather_mode_button"
                            android:text="weather_mode_desc"
                            android:textColor="#8b8b8b"
                            android:textSize="14sp" />

                        <Button
                            android:id="@+id/weather_mode_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="mode_button" />

                    </RelativeLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/wifi_mode_card"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="14dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:elevation="4dp"
                        android:padding="10dp">

                        <ImageView
                            android:id="@+id/wifi_mode_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_marginEnd="10dp"
                            android:layout_marginTop="6dp" />

                        <TextView
                            android:id="@+id/wifi_mode_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_toEndOf="@+id/wifi_mode_icon"
                            android:paddingBottom="5dp"
                            android:text="wifi_mode"
                            android:textSize="20sp" />

                        <TextView
                            android:id="@+id/wifi_mode_desc"
                            android:layout_width="220dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/wifi_mode_title"
                            android:layout_toEndOf="@+id/wifi_mode_icon"
                            android:layout_toStartOf="@id/wifi_mode_button"
                            android:text="wifi_mode_desc"
                            android:textColor="#8b8b8b"
                            android:textSize="14sp" />

                        <Button
                            android:id="@+id/wifi_mode_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="mode_button" />

                    </RelativeLayout>

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/temp_mode_card"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="14dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#fff"
                        android:elevation="4dp"
                        android:padding="10dp">

                        <ImageView
                            android:id="@+id/temp_mode_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_marginEnd="10dp"
                            android:layout_marginTop="6dp" />

                        <TextView
                            android:id="@+id/temp_mode_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_toEndOf="@+id/temp_mode_icon"
                            android:paddingBottom="5dp"
                            android:text="temp_mode"
                            android:textSize="20sp" />

                        <TextView
                            android:id="@+id/temp_mode_desc"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/temp_mode_title"
                            android:layout_toEndOf="@+id/temp_mode_icon"
                            android:layout_toStartOf="@id/temp_mode_button"
                            android:text="temp_mode_desc"
                            android:textColor="#8b8b8b"
                            android:textSize="14sp" />

                        <Button
                            android:id="@+id/temp_mode_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentEnd="true"
                            android:layout_centerVertical="true"
                            android:text="mode_button" />

                    </RelativeLayout>

                </android.support.v7.widget.CardView>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>
0
ʍѳђઽ૯ท 17 Сен 2018 в 08:22

Попробуйте поместить свою панель инструментов в appbarlayout в xml и проверьте. Что-то вроде этого:

<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:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>
0
abhradeep ghosh 16 Сен 2018 в 12:29