API等级19的抽屉活动

问题描述 投票:1回答:2

我已将最小SDK从21更改为19,在19API中,未显示抽屉活动中的切换按钮和工具栏(以打开菜单)。

API> 21toggle and Toolbar appear

API 19 nothing shows

我的布局是两个文件:app_bar_main和content_main

APP_BAR_MAIN

    <?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="gchmapplications.hermandadsantamariavictoria.Actividades.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"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

CONTENT_MAIN

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/contenedor"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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="wrap_content"
            android:layout_weight="1"
            />

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

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

我尝试过其他配置并没有解决问题,但问题是它存在于视图中但是显示出“屏幕外”,因为当我更改某些值时它出现在工具栏上方enter image description here


我注意到从零开始我在API19中遵循的教程(https://www.youtube.com/watch?v=0aLFCpaoOZo)没有出现,所以问题是我遵循的教程。

android menu navigation-drawer android-4.4-kitkat
2个回答
1
投票

尝试使用下面提到的代码来实现您的实现:

<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/myDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="in.assamtourism.atdc_demo.MainActivity"
    tools:openDrawer="left">

<RelativeLayout>

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#b71c1c"
            android:theme="@style/DrawerArrowStyle"
            app:layout_scrollFlags="scroll|enterAlways" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#8c000000"
        android:gravity="center_horizontal"
        android:padding="20dp"
        android:textColor="#ffffff"/>

<!--All Your UI codes should be here-->

</RelativeLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/myNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:fitsSystemWindows="true"
        app:itemIconTint="#e53935"
        app:headerLayout="@layout/nav_header" />

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

然后在“YourActivity”.java文件中声明这些变量:

Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;

在onCreate方法中,添加以下行:

        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.myDrawerLayout);
        navigationView = findViewById(R.id.myNavigationView);
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.nav_menu);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
        );
        drawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();

完成后,根据您的需求进行定制。


1
投票

我不知道为什么现在的布局比底部的屏幕大。在大多数视图中我有5dp的余量,现在它削减了边距和一点点内容......

这是实际的activity_main

<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:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:openDrawer="start">

<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="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:titleTextColor="@android:color/white" />

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:theme="@style/AppTheme.AppBarOverlay" />

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

</LinearLayout>

<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/nav_header_main"
    app:itemIconTint="#981e73be"
    app:itemTextColor="#2E2E2E"
    app:menu="@menu/activity_main_drawer" />

© www.soinside.com 2019 - 2024. All rights reserved.