【问题标题】:Draw image under Android navigation bar在Android导航栏下绘制图像
【发布时间】:2017-04-11 17:42:53
【问题描述】:

我正在尝试创建一个布局,其中我的根视图占据全屏并绘制在状态/导航栏下。一切正常,直到我有一个高大的弹出窗口(图像中的那个有 60 个项目)。窗口远远低于窗口,无法滚动。如何在不让弹出窗口超出屏幕的情况下让我的布局扩展到导航栏下方?

罪魁祸首是

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

问题是如果没有那个标志,我似乎无法在导航栏下绘制我的视图。相反,我只能设置android:windowBackground(显示为绿色)。

这是我正在使用的布局:

<android.support.constraint.ConstraintLayout
    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:background="@color/colorAccent"
    android:fitsSystemWindows="false"
    tools:context="com.example.fullscreendemo.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.v7.widget.AppCompatSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/spinner_values"
        android:clipChildren="true"
        app:layout_constraintTop_toBottomOf="@+id/text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <View
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="@drawable/bg_nav_bar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

主要活动:

class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecoreView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

  • 嗨,您在活动中使用的是哪种风格?没有这个标志你可以实现透明条你不是有原因吗?
  • 我正在使用Theme.AppCompat.Light.NoActionBar 栏是透明的,即使没有标志,问题是我只能在它下面用颜色或使用android:windowBackground 绘制。从图中可以看出,栏下方的颜色是绿色,是窗口背景,而不是导航栏背景。
  • 我把它用作 android 19 的样式,如果你想试一试,它有同样的效果,而且你修好了它
  • 谢谢,我会试一试(但我必须适应它,因为 AppCompatActivity 必须使用 AppCompat 主题)。

标签: android


【解决方案1】:

好的,很明显,在发布此内容后,我就知道发生了什么。 Window.setFlags 的文档说在调用setContentViewgetDecoreView 时将使用FLAG_LAYOUT_IN_SCREENFLAG_LAYOUT_INSET_DECOR。正是插图导致我的活动底部“提升”。我可以通过简单地删除我的活动中的大部分代码并只留下来避免所有这些

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);

编辑 当使用没有软导航栏的设备时,以上内容还不足以适应所有设备的使用:

private void setFullScreenFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    if (!hasSoftNavigationBar()) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    AndroidBug5497Workaround.assistActivity(this);
}

private boolean hasSoftNavigationBar() {
    Display display = getWindowManager().getDefaultDisplay();
    Point realSize = new Point();
    Point displaySize = new Point();

    display.getRealSize(realSize);
    display.getSize(displaySize);
    return !realSize.equals(displaySize);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多