android - 仅显示操作栏的下半部分(并且在状态栏后面)

标签 android android-actionbar

由于某种原因,只显示了 ActionBar 的下半部分,而显示的那一半在状态栏后面。为什么会这样? 这只发生在 DrawerActivity 中。

这一定是新版 Android 的问题,因为我注意到 Android 5.0.2 和 6.0 中的错误。在 Android 4.4.4 中没有问题。

我正在从 android.support.v7.app.AppCompatActivity 扩展我的 Activity 并在其中实现一些 fragment 。

这是我的应用配置:

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.mypackage.myapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
}

DrawerActivity 的布局

<?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="false"
    tools:openDrawer="start">

    <include 
        layout="@layout/app_bar_drawer" 
        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_drawer" 
        app:menu="@menu/activity_drawer_drawer" />

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

样式.xml :

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

</resources>
DrawerActivity

onCreate 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Setting floating button
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //Setting up default fragment.
    Fragment fragment = HomeFragment.newInstance(null, null);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.mainFrame, fragment);
    ft.commit();

}

为了更好的理解,这里有一个截图:

Action bar behind the status bar

谢谢!

最佳答案

在你的oncreate中添加这一行

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    //then set status bar color or any thing else
}

关于android - 仅显示操作栏的下半部分(并且在状态栏后面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437579/

相关文章:

java - 滚动 RecyclerView 时出现滞后/错误的 y 坐标(OnscrollListener)

java - 更改 actionBar 下拉菜单背景颜色

android - 无法在 xamarin MVVMCROSS 中从 android.support.v4.app.Fragment 转换为 android.app.Fragment

android - 广播接收器和服务有什么区别?

java - 待定 Intent 不是打开 Activity

java - 我们如何在不打开联系人应用程序本身的情况下将新联系人插入 Android 联系人中?

java - 找不到参数的方法 applicationId() ?

java - Android项目连接WampServer的IP地址是多少?

android - 如何在 Android 中使用这个库?

android - 如何防止 Activity 中的操作栏向上移动和消失?