【问题标题】:Bottom TabLayout leaving space at bottom底部 TabLayout 在底部留下空间
【发布时间】:2017-12-08 16:47:52
【问题描述】:

我遇到了一个问题。我有一个自定义的工具栏布局,在我的活动 xml 中,我包含了这个工具栏。我的活动结构是:

  1. 顶部的工具栏布局使用include 标签。
  2. ViewPager
  3. TabLayout at bottom 以获取底部选项卡。

我面临的问题是,在某些设备上,它表现得很完美,但在一些设备中(在 moto G 系列设备上测试),底部标签留出了空间。

请参考以下图片:

在普通手机中显示预期的 UI

在 MOTO G 系列中

xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efeded"
android:weightSum="11"
android:orientation="vertical">

<include layout="@layout/custom_tool_bar" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"

    android:layout_height="0dp"

    android:layout_weight="10" />

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    style="@style/BottomTabLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#fff"
    android:layout_weight="1"
    app:tabIndicatorColor="@color/colorPrimaryDark"
    app:tabSelectedTextColor="@color/colorPrimaryDark"
    />

</LinearLayout>

【问题讨论】:

标签: android android-layout android-tablayout android-tabs android-design-library


【解决方案1】:

对于底部标签,您可以使用 bottomnavigationview,它可以提供更好的性能,并且可以在 MOTO 设备中正常工作。检查这个

https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html

【讨论】:

    【解决方案2】:
    1. 删除父 LinearLayout 中的 android:weightSum=11 属性;
    2. 将 ViewPager 的 android:layout_weight=10 属性更改为 android:layout_weight=1
    3. 移除 TabLayout 的 android:layout_weight=1 属性

    然后再试一次。

    【讨论】:

    • 看起来是完美的答案。
    • 我做了,但没有帮助。它仍然在底部留下一些空间
    • @TheBat 这听起来很奇怪。你有其他设备可以测试吗?这只发生在 Moto G 上吗?另外我注意到第二张截图没有软导航按钮,和你的问题有关系吗?
    • @IntelliJAmiya 我已经写了一个答案,请看一下。
    • @TheBat 据我所知,出现额外填充的原因是您编写的用于检测系统导航栏软键是否存在的覆盖方法。它与您之前发布的 xml 布局无关(而且我认为 xml 布局实际上可以在没有检测软导航键方法的情况下工作)。也许你应该试着问一个关于它的新问题,虽然我以前见过一些类似的问题,而且它实际上似乎因设备而异,不同的 rom 也不同。
    【解决方案3】:

    我发现了它为什么会发生的问题。我在我的活动中使用了一个库 - ResideMenu,它具有检测设备是否具有硬件返回和菜单按钮或数字按钮的功能。

    @Override
    protected boolean fitSystemWindows(Rect insets) {
    
        int bottomPadding = viewActivity.getPaddingBottom() + insets.bottom;
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    
        if (!hasBackKey || !hasHomeKey) {//there's a navigation bar
            bottomPadding += getNavigationBarHeight();
        }
    
        this.setPadding(viewActivity.getPaddingLeft() + insets.left,
                viewActivity.getPaddingTop() + insets.top,
                viewActivity.getPaddingRight() + insets.right,
                bottomPadding);
        insets.left = insets.top = insets.right = insets.bottom = 0;
        return true;
    }
    

    这不能正常工作。对于某些设备,它可以正常工作,但对于某些设备,它无法确定按钮。因此,在某些设备中,它正在上升,为数字按钮留下了空间。 无论如何,我找不到解决方案,尝试了一些 StackOverflow 但没有成功,如果有人发现了什么,请发布。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2017-11-23
      • 2014-05-19
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多