【问题标题】:application content goes behind the navigation bar in android L应用程序内容位于 android L 中的导航栏后面
【发布时间】:2015-04-02 21:50:52
【问题描述】:

如您所见,我的“得到它”按钮位于导航栏后面。无法修复它!!!我试过了

<item name="android:fitsSystemWindows">true</item>  

以及在布局文件中设置。

我在 value-21 中的主题是:

 <style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:fitsSystemWindows">true</item>
    </style>

整个应用程序的所有屏幕都是相同的情况。

请帮忙。

【问题讨论】:

  • 尝试:View.bringToFront();
  • @user863873:layout 不应该使用这个空间。它应该在这些按钮的上方。
  • 你能把“知道”按钮放在导航栏前面吗?我似乎无法让它发生。你能帮助我吗?您在窗口中添加了哪些其他标志?我添加了你所有的标志,但导航栏总是在我的活动前面。
  • @niteshgoel 你能回答吗?
  • 是的,它是这样工作的。请检查我的答案,我是如何解决的。

标签: android performance android-layout android-fragments android-5.0-lollipop


【解决方案1】:

这是解决方案。

大多数布局都可以通过在 values-v21 style.xml 中添加这些属性来解决

<item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:fitsSystemWindows">true</item>

对于其他人,我已经计算了导航栏的高度并为我的视图添加了边距。

public static int getSoftButtonsBarSizePort(Activity activity) {
    // getRealMetrics is only available with API 17 and +
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
        else
            return 0;
    }
    return 0;
}

注意:通过使用上述解决方案一切正常,但我也在我的应用程序中使用 PopupWindow。PopupWindow 的布局在 android L 中变得混乱。寻找问题和解决方案 @987654321 @

【讨论】:

  • 我在我的样式中添加了以下内容 truetrue 它有效!导航栏后面没有绘制任何内容。除了现在我的导航栏是深灰色而不是黑色。将 TranslucentNavigation 设置为 false 会使条变黑,但内容会在其后面绘制。认为您可以解释为什么这些属性有效,以及如何使导航栏保持黑色?
  • 您已经定义了方法,但您没有告诉我们如何使用或调用它。您能否告诉我们从哪里调用该方法以及如何使用它返回的内容。
  • 这有助于但如何判断用户何时打开或关闭软导航键栏?这样我们就可以调整填充的大小?
【解决方案2】:

就是这么简单, 只需将此行添加到您的父布局 xml:

android:fitsSystemWindows="true"

【讨论】:

  • 不能在 pie 上工作可能是因为 android M 就不能工作
  • 为我工作,在运行 Android Pie 的 Android 模拟器中测试
【解决方案3】:

为了获得良好的用户体验,您不需要使用 android:windowTranslucentNavigation 阻止导航键区域

这里是更好的解决方案,如果您使用的是 ResideMenu 库,那么只需在 ResideMenu.java 中添加此方法

  @Override
protected boolean fitSystemWindows(Rect insets) {
    int bottomPadding = insets.bottom;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            bottomPadding += resources.getDimensionPixelSize(resourceId);
        }
    }
    this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
            viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
    insets.left = insets.top = insets.right = insets.bottom = 0;
    return true;
}

如果您使用的是 SlidingMenu 库,则通过以下方式将 mod 更改为 SLIDING_CONTENT

menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

希望它能节省您的时间

【讨论】:

  • +1 由于 ResideMenu,我遇到了同样的问题,这个答案在浪费了这么多时间后帮助了我。谢谢!
  • 我也在使用驻留菜单。为 Android P 导航手势功能做什么。在那个导航栏隐藏然后我得到导航栏高度而不是零。你能帮帮我吗?
【解决方案4】:

只需在 Activity 的 onCreate() 中设置它,同时设置内容视图:

int mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
int mScreenHeight = getWindowManager().getDefaultDisplay().getHeight();
View view = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(view, new ViewGroup.LayoutParams(mScreenWidth, mScreenHeight));

【讨论】:

    【解决方案5】:

    Activity 的 onCreate 方法中使用此代码(如果您有 BaseActivity,您可以在此处执行此操作,以便所有活动都应用此代码):

    ViewCompat.setOnApplyWindowInsetsListener(
                findViewById(android.R.id.content), (v, insets) -> {
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                    params.bottomMargin = insets.getSystemWindowInsetBottom();
                    return insets.consumeSystemWindowInsets();
                });
    

    当一个布局有这个标志时,这意味着这个布局正在要求系统应用窗口插入。在我们的例子中,这意味着 FrameLayout 希望应用一个等于状态栏高度的填充。

    【讨论】:

    • 对于 pre-lambda:.setOnApplyWindowInsetsListener (findViewById(android.R.id.content), new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { // .. } } );
    【解决方案6】:

    我在上面尝试了很多答案,但没有一个对我有用。我已经将我的相对布局包裹在另一个线性布局中,其中我的按钮(必须附加到底部)。它神奇地为我工作。

    如果有人可以为此添加原因,那将更有帮助。

    【讨论】:

      【解决方案7】:

      在大多数情况下,默认,导航栏应始终位于应用下方,除非您触发了设置。

      如果这是您要确保您没有运行此行的行为:

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

      【讨论】:

      • 在某些移动设备中,这是导致问题的原因。
      【解决方案8】:

      softInputModepopupWindow 设置为SOFT_INPUT_ADJUST_RESIZE 可解决此问题。

      【讨论】:

        【解决方案9】:

        在你的java的setcontent视图之前试试这个,我的问题已经解决了`

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);`
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-11
          • 1970-01-01
          • 2017-04-11
          • 2021-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多