我几天来一直在努力解决这个问题,但还没有找到解决方案。我有一个带有工具栏和几个片段的NavigationDrawer活动,就像那样简单。令人费解的是,工具栏仍然位于状态栏仅适用于Android 4.4 (API 19)。
我已经使用了我找到的所有内容,我甚至从其他应用程序中复制了代码,但这些代码工作正常但没有任何改变。
这是所有相关代码:
的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'
android {
compileSdkVersion 24
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "xxx"
minSdkVersion 16
targetSdkVersion 24
versionCode 3
versionName "1.0"
}
signingConfigs {
...
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
debuggable true
useProguard false
applicationIdSuffix ".debug"
}
}
}
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
maven { url "https://clojars.org/repo/" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true;
}
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.google.android.gms:play-services-gcm:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'
compile 'com.squareup:android-times-square:1.6.5@aar'
compile 'frankiesardo:icepick:3.2.0'
provided 'frankiesardo:icepick-processor:3.2.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'net.danlew:android.joda:2.9.3'
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1@aar'
}
Android清单
<application
android:name=".MyApplication"
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="false"
android:theme="@style/Base.AppTheme">
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/Base.AppTheme.Main"
android:windowSoftInputMode="stateHidden"/>
</application>
的themes.xml
<resources>
<!-- Base -->
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColor_dark</item>
<item name="colorAccent">@color/accentColor</item>
<item name="android:windowBackground">@color/background_activity</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fontFamily">sans-serif</item>
</style>
<!-- Main Activity -->
<style name="Base.AppTheme.Main">
<item name="android:textColorSecondary">@android:color/white</item>
</style>
</resources>
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: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_drawer_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:background="@color/background_navigationDrawer"
android:fitsSystemWindows="true"
app:itemBackground="@drawable/drawer_item"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/activity_drawer_drawer">
<TextView
android:id="@+id/nav_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:padding="16dp"
android:textColor="@android:color/white"
tools:text="Version 0.0.0"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_drawer_main.xml
<?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"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main">
<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.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextView.Toolbar" />
<ImageView
android:id="@+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/content_desc_logo"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/logo_toolbar"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/toolbat_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:visibility="gone" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<include layout="@layout/component_loading_spinner" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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_frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.MainActivity"
tools:showIn="@layout/app_bar_drawer_main" />
fragment_browser.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">
...
</LinearLayout>
</ScrollView/>
我做错了什么?我需要在API 19上做些什么才能使fitSystemWindows
工作,Toolbar
位于状态栏下方?
答案 0 :(得分:1)
最后我解决了。问题是我覆盖了themes
中的styles-v19.xml
,我添加了这一行:
<item name="android:windowTranslucentStatus">true</item>
删除后,工具栏完美无缺。我会更新样式代码段以包含此内容,以防有人发现这篇文章有用。