android 底部软NavigationBar与我的ListView重叠

ldfqzlk8  于 2023-05-05  发布在  Android
关注(0)|答案(6)|浏览(250)

我在Nexus 5(Android 5)上运行了我的应用程序,但我遇到了一个问题,即底部的软NavigationBar与ListView的最后一项重叠。我尝试将fitsSystemWindows添加到我的样式和ListView中,但没有成功。
我的布局的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/sf4l"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/sf4l" />
</LinearLayout>
nsc4cvqm

nsc4cvqm1#

将其添加到values-v21目录中的themes.xml中:

<item name="android:windowDrawsSystemBarBackgrounds">false</item>

示例(我使用AppCompat作为操作栏):

<style name="Theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="homeAsUpIndicator">@drawable/new_indicator</item>
    <item name="android:homeAsUpIndicator">@drawable/new_indicator</item>
    <item name="actionModeBackground">@android:color/black</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
4nkexdtk

4nkexdtk2#

这是因为listView的高度等于全屏的高度,但是操作栏将布局推低了几个像素,这导致了导航按钮的布局重叠。
如果你在操作栏下有全屏的片段容器,这也会发生。
解决方法是测量屏幕和操作栏的高度,然后设置父视图的高度。示例:

@Override
public void onWindowFocusChanged (boolean hasFocus) {
    LinearLayout lMain = (LinearLayout) findViewById(R.id.lMain);
    lMain.getLayoutParams().height = lMain.getHeight() - getNavigationBarHeight();
}
  • 你必须在布局已经被绘制/渲染之后执行此操作。In onCreate或onResume太早。在onWindowFocusChanged()中这样做可能有点大材小用,但它确实有效。
nzkunb0c

nzkunb0c3#

我在这篇文章中尝试了公认的答案,像许多人一样,它对我不起作用。
然而,下面的工人对我来说。

<item name="android:windowTranslucentNavigation">false</item>

我放在styles-21.xml中
它所做的是使软导航栏有一个坚实的背景,不知何故,现在组件被正确呈现。

eoigrqb6

eoigrqb64#

这个答案是一种混合其他答案和我的试验,以实现这一点从API-15开始。

适用于API-21+

在 * styles.xml(v21)* 中,将下面添加到活动的样式中

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     ....
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

适用于API-19+

在 * styles.xml(v19)* 中,将下面添加到活动的样式中

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     ....
    <item name="android:windowTranslucentNavigation">false</item>
</style>

适用于API-15+

在您的活动中覆盖以下内容

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // return for API-19+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        return;

    // Customize Activity to not overlap with the bottom software buttons
    // navigation bar (back, home, & menu)
    boolean hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

    if (!hasMenuKey && !hasBackKey) { // check if this device has a bottom navigation bar
        ConstraintLayout rootLayout = findViewById(R.id.activity_root); // my activity root layout
        int NAVIGATION_BAR_HEIGHT = 70;
        rootLayout.getLayoutParams().height = rootLayout.getHeight() - NAVIGATION_BAR_HEIGHT;
    }
}
5f0d552i

5f0d552i5#

非常简单的解决方案。使用ConstraintLayout即可。将俯视图的底边与仰视图的顶边对齐。
样品如下:
联系我们

<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
    android:id="@+id/list_item"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:divider="@color/black"
    android:dividerHeight="2dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"
    app:layout_constraintHorizontal_bias="1.0"></ListView>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />
    </android.support.constraint.ConstraintLayout>

输出设计:-

ubof19bj

ubof19bj6#

没有人提到插图。

View.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            int bottomInset = insets.getSystemWindowInsetBottom();
            // Do something with the bottom inset value, such as adjusting the layout
            v.getPaddingBottom();// useless.
            insets.getSystemWindowInsetBottom(); // correct
            insets.getStableInsetBottom();

            return insets;
        }
    });

这段代码只是给读者指出正确的方向,并不意味着功能正确。post

相关问题