Панель инструментов не отображается с помощью навигационного ящика. Не в студии android и в устройстве

Я использую ящик навигации в своем приложении. Я хочу добавить в него панель инструментов, но панель инструментов не отображается. он просто дает оценку размера предварительного просмотра дизайна. Как показано на рисунке

Я пробовал почти все, что мог, но он не работает. Заранее спасибо:)

xml file

 <?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.mrvirk.urduapp.MainActivity"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

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




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="8">


        <android.support.v7.widget.GridLayout


            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:columnCount="2"
            app:rowCount="4">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/green3"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

                <ImageView
                    android:id="@+id/learning"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/learning"
                    android:layout_gravity="center"
                    />


                <TextView
                    android:id="@+id/txtLearning"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/i_name"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/yellow"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">


                <ImageView
                    android:id="@+id/acheivements"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/acheivements"

                    />


                <TextView
                    android:id="@+id/txtAcheivements"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/i_national"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/red"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnSpan="2"
                app:layout_rowWeight="1"
                app:layout_columnWeight="1">

                <ImageView
                    android:id="@+id/quiz"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitCenter"
                    android:src="@drawable/quiz" />

                <TextView
                    android:id="@+id/txtQuiz"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Quiz"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/blue2"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

                <ImageView
                    android:id="@+id/setting"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/setting" />

                <TextView
                    android:id="@+id/txtSetting"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Setting"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/tea"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

                <ImageView
                    android:id="@+id/share"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/share" />

                <TextView
                    android:id="@+id/txtshare"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Share"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/org"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

                <ImageView
                    android:id="@+id/about"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/about" />

                <TextView
                    android:id="@+id/txtAbout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="About"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/green3"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="1dp"
                app:layout_columnWeight="1"
                app:layout_rowWeight="1">

                <ImageView
                    android:id="@+id/moreApps"
                    android:layout_width="match_parent"
                    android:layout_height="@android:dimen/notification_large_icon_width"
                    android:scaleType="fitCenter"
                    android:src="@drawable/moreapps" />

                <TextView
                    android:id="@+id/txtMoreApps"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="More Apps"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>
        </android.support.v7.widget.GridLayout>

    </LinearLayout>


    </FrameLayout>


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

Панель инструментов в основном действии

Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.done);

Menifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mrvirk.urduapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <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/Theme.AppCompat.Light.NoActionBar">
        <activity android:name="com.example.mrvirk.urduapp.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
0
задан 13 August 2018 в 14:29

4 ответа

Android рисует виды сверху вниз, поэтому; A-B-C- вы видите 3 соответствующих вида в своем DrawerLayout. теперь A - это панель инструментов, которая сначала рисуется с шириной полосы ширины, соответствующей ширине, и высотой, соответствующей самому контенту, B рисуется с шириной по ширине и ширине по высоте и т. д. и т. д. это означает, что ваш FrameLayout будет нарисован вторым в последовательности рисунков и поверх панели инструментов, но, поскольку он был закрыт вами, вы не видите, что он есть, если вы принесете его ниже, ваша панель инструментов рисуется второй подряд, следовательно, вы видите это.

Кредиты: @Elltz

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

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <FrameLayout

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



        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="8">


            <android.support.v7.widget.GridLayout


                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:columnCount="2"
                app:rowCount="4">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/green3"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">

                    <ImageView
                        android:id="@+id/learning"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/learning"
                        android:layout_gravity="center"
                        />


                    <TextView
                        android:id="@+id/txtLearning"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/i_name"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/yellow"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">


                    <ImageView
                        android:id="@+id/acheivements"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/acheivements"

                        />


                    <TextView
                        android:id="@+id/txtAcheivements"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/i_national"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/red"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnSpan="2"
                    app:layout_rowWeight="1"
                    app:layout_columnWeight="1">

                    <ImageView
                        android:id="@+id/quiz"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:scaleType="fitCenter"
                        android:src="@drawable/quiz" />

                    <TextView
                        android:id="@+id/txtQuiz"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Quiz"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/blue2"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">

                    <ImageView
                        android:id="@+id/setting"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/setting" />

                    <TextView
                        android:id="@+id/txtSetting"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Setting"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/tea"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">

                    <ImageView
                        android:id="@+id/share"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/share" />

                    <TextView
                        android:id="@+id/txtshare"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Share"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/org"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">

                    <ImageView
                        android:id="@+id/about"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/about" />

                    <TextView
                        android:id="@+id/txtAbout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="About"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/green3"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    app:layout_columnWeight="1"
                    app:layout_rowWeight="1">

                    <ImageView
                        android:id="@+id/moreApps"
                        android:layout_width="match_parent"
                        android:layout_height="@android:dimen/notification_large_icon_width"
                        android:scaleType="fitCenter"
                        android:src="@drawable/moreapps" />

                    <TextView
                        android:id="@+id/txtMoreApps"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="More Apps"
                        android:textColor="@color/white"
                        android:textStyle="bold" />

                </LinearLayout>
            </android.support.v7.widget.GridLayout>

        </LinearLayout>

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


    </FrameLayout>


    <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>
0
ответ дан 15 August 2018 в 17:04
  • 1
    Большое спасибо приятелю. Работал для меня :) – Muhammad Ansab Virk 15 August 2018 в 09:52
  • 2
    Проблема разрешена, но панель инструментов отображается над элементом сетки (поверх первых фрагментов). вышеупомянутый ответ разрешил всю проблему. Спасибо за вашу помощь тоже :) – Muhammad Ansab Virk 15 August 2018 в 10:10

Мое предложение - заменить FrameLayout и вместо этого использовать RelativeLayout. Действительно, RelativeLayout должен содержать Toolbar и LinearLayout.

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

    <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"
        />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:weightSum="8">
 ....
</LinearLayout>

Тот факт, что LinearLayout ниже панели инструментов выполняется следующее:

android:layout_below="@+id/toolbar"
0
ответ дан 15 August 2018 в 17:04

Измените FrameLayout на LinearLayout или CoordinateLayout

, если вы используете LinearLayout, затем

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

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

или если вы используете CoordinateLayout, тогда,

<android.support.design.widget.CoordinatorLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    />
</android.support.design.widget.AppBarLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:weightSum="8">
    .......
</LinearLayout>
</LinearLayout>
0
ответ дан 15 August 2018 в 17:04

main layout:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="@dimen/size300"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemBackground="@drawable/drawer_item_background_selector"
    app:itemTextAppearance="?android:attr/textAppearanceSmall"
    app:itemTextColor="@android:color/black"
    app:menu="@menu/activity_main_drawer">

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

app_bar_main layout:

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.appsdevelopment.stylus.aci.allor_shathi.activity.MainActivity">

<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"
        app:titleTextAppearance="@style/Toolbar.TitleText" />

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

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/size_minus45"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
0
ответ дан 15 August 2018 в 17:04

Другие вопросы по тегам:

Похожие вопросы: