简体   繁体   English

在Android KitKat上看不到ActionBar

[英]ActionBar not visible on Android KitKat

I am trying to implement Toolbar using android design library. 我正在尝试使用android设计库来实现工具栏。 The Output works as expected on Lollipop and Marshmallow devices but it does not show ToolBar on Kitkat devices. 输出在棒棒糖和棉花糖设备上可以正常工作,但在Kitkat设备上不显示工具栏。

I have seen similar posts on stack overflow( here_1 , here_2 ) and tried to implement the changes listed. 我在堆栈溢出( here_1here_2 )上看到了类似的帖子,并尝试实现列出的更改。 However, I am unable to get this working on Kit Kat devices. 但是,我无法在Kit Kat设备上使用它。

I have included the following in my build.gradle file: 我的build.gradle文件中包含以下内容:

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'

Here is my main_Activity.xml file: 这是我的main_Activity.xml文件:

<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_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/Theme.MyMovie"
    tools:context=".MainActivity">


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


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

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

Here is my include_grid_viewpager.xml file: 这是我的include_grid_viewpager.xml文件:

<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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Chalisa">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            />

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

    <GridView
        android:id="@+id/gridview1"
        android:columnWidth="80dp"
        android:paddingTop="?attr/actionBarSize"
        android:gravity="center_horizontal"
        android:background="@drawable/stroke"
        android:numColumns="3"
        android:stretchMode="spacingWidthUniform"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </GridView>

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

and Here is my style.xml file 这是我的style.xml文件

<resources>

    <style name="Theme.Mytheme" parent="Base.Theme.DesignDemo">
    </style>

    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#FF0000</item>
        <item name="colorPrimaryDark">#FC1501</item>
        <item name="colorAccent">#FF0000</item>
        <item name="android:windowBackground">@color/window_background</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="Widget.CardContent" parent="android:Widget">
        <item name="android:paddingLeft">16dp</item>
        <item name="android:paddingRight">16dp</item>
        <item name="android:paddingTop">24dp</item>
        <item name="android:paddingBottom">24dp</item>
        <item name="android:orientation">vertical</item>
    </style>

</resources>

Here is my manifest file: 这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="aps.com.nap" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Mytheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DetailData"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"/>
        <activity android:name=".myDetail"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"/>

    </application>

</manifest>

I have spent more than 4 hours digging through internet and stack overflow responses. 我花了4个多小时来浏览Internet和堆栈溢出响应。 I believe I am making some minor mistake in my XML. 我相信我在XML中犯了一些小错误。

Request you all to provide any information that will help me resolve this Issue at the earliest. 要求大家提供任何可帮助我尽早解决此问题的信息。

Thank you for all the help. 感谢您的所有帮助。

I had to change my include_grid_viewpager.xml as follows to get it working. 我必须按照以下步骤更改include_grid_viewpager.xml才能使其正常运行。 moved the grid inside the AppBar Layout, so that it is below the action bar. 在AppBar布局内移动了网格,使其位于操作栏下方。 The grid view was overlaying the action bar. 网格视图覆盖在操作栏上。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways"
            />

        <GridView
            android:id="@+id/gridview1"
            android:columnWidth="110dp"
            android:gravity="center_horizontal"
            android:background="@drawable/stroke"
            android:numColumns="3"
            android:stretchMode="spacingWidthUniform"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </GridView>

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



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

Thank you for all the support. 感谢您的支持。

I have got the same problem. 我有同样的问题。 In my case, appending dummy view before AppBarLayout solved the problem. 就我而言,在AppBarLayout之前附加虚拟视图AppBarLayout解决该问题。

Following is my own case: 以下是我自己的情况:

[Before] [之前]

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <ProgressBar
            android:id="@+id/eya_pb_activity_base_viewer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="?attr/actionBarSize"
            android:visibility="gone" />

    </FrameLayout>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/eya_abl_activity_base_viewer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

    <!-- Other views... -->

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

[After] [后]

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy view -->
    <FrameLayout
        android:id="@+id/eya_fl_activity_viewer_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/eya_abl_activity_base_viewer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

    <!-- Other views... -->

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

Please review inside onCreate() of your activity, have you called the following yet? 请在您的活动的onCreate()内部进行检查,您是否致电了以下内容?

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);

Moreover, post your AndroidManifest.xml for more info. 此外,发布您的AndroidManifest.xml以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM