My Toolbar is not shown on API level 19 (Kitkat) while it is shown for API level 21

牧云@^-^@ 提交于 2019-12-12 16:08:08

问题


My Code is not showing the Toolbar on Kitkat .
Here is my ScreenShot for both the version of Android.

Kitkat version:

Lollipop version:

What might be the reason behind this.

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.global.market">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/image_front"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".detailactivity"
            android:label="@string/app_name" />
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <meta-data android:name="com.google.android.gms.version" android:value="6111000" />
    </application>

</manifest>

Styles.xml file

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBarOverlay">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

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"
    xmlns:ads="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"
    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="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        android:clickable="true">

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

    <ExpandableListView
        android:id="@+id/exp_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="160dp"
        android:indicatorRight="?android:attr/expandableListPreferredItemIndicatorRight"
        android:background="@android:color/white"
        android:footerDividersEnabled="true"
        android:groupIndicator="@null">
    </ExpandableListView>

            <!--<ListView-->
                <!--android:background="@android:color/white"-->
                <!--android:id="@+id/menuList"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent"-->
                <!--android:layout_marginTop="10dp" />-->
        </LinearLayout>
    </android.support.design.widget.NavigationView>

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

MainActivity.java

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                if (toolbar != null) {
                    toolbar = (Toolbar) findViewById(R.id.toolbar);
                    setSupportActionBar(toolbar);
                   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                }
}

Build.gradle file

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    ext {
        supportLibVersion = '23.4.0'  // variable that can be referenced to keep support libs consistent
    }

    defaultConfig {
        applicationId "com.global.market"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.android.support:cardview-v7:23.0.+'
    compile 'com.android.support:recyclerview-v7:23.0.+'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.github.gabrielemariotti.cards:cardslib-core:2.1.0'
    compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.1.0'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
    compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
    compile 'com.google.android.gms:play-services:6.1.11'



}

回答1:


AppBar by default has elevation which works after lollipop. So it is always visible despite the layouts hierarchy. But on lower version (below lollipop) the toolbar might be drawn below some other layout.

So reorder your layout items to make the toolbar visible.




回答2:


Change your style.xml to this

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

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

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

and in MainActivity.java you are initiliaze two time ToolBar so remove second line this

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

you don't need it.




回答3:


Try this

Update style.xml

<resources>

    <style name="AppTheme" parent="AppTheme.Base">

    </style>

    <style name = "AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resource>

and also update the activity theme in AndroidManifest.xml file

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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


来源:https://stackoverflow.com/questions/38454670/my-toolbar-is-not-shown-on-api-level-19-kitkat-while-it-is-shown-for-api-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!