【问题标题】:ViewPager under of BottomNavigationBottomNavigation 下的 ViewPager
【发布时间】:2020-08-09 12:50:56
【问题描述】:

我在 viewpager 中有列表,底部有音频播放器。但是我的查看器并没有结束音频播放器的启动。我在 viewpager 中看不到我最后的 2 - 3 个项目。而且我不想使用MarginBottom

一些截图

我的活动.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- AppBar Layout   -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <!-- Tab Layout for creating tabs -->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>
    <!-- Helps handing the Fragments for each Tab -->



    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.BottomNavigationView
        android:layout_width="fill_parent"
        android:layout_height="90dp"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/viewPager"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_gravity="bottom"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <android.support.design.widget.NavigationView

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:paddingLeft="10dp"
            android:layout_gravity="center"
            android:paddingRight="10dp"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/btnBackward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="50dp"
                android:layout_marginBottom="4dp"
                android:src="@android:drawable/ic_media_rew" />

            <ImageButton
                android:id="@+id/btnPlay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/btnBackward"
                android:layout_marginLeft="20dp"
                android:layout_toRightOf="@+id/btnBackward"
                android:src="@android:drawable/ic_media_play" />

            <ImageButton
                android:id="@+id/btnPause"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/btnPlay"
                android:layout_marginLeft="20dp"
                android:layout_toRightOf="@+id/btnPlay"
                android:src="@android:drawable/ic_media_pause" />

            <ImageButton
                android:id="@+id/btnForward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/btnPause"
                android:layout_marginLeft="20dp"
                android:layout_toRightOf="@+id/btnPause"
                android:contentDescription="@+id/imageButton3"
                android:src="@android:drawable/ic_media_ff" />

            <TextView
                android:id="@+id/txtStartTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/sBar"
                android:text="0 min, 0 sec" />

            <SeekBar
                android:id="@+id/sBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/btnBackward"
                android:layout_toLeftOf="@+id/txtSongTime"
                android:layout_toRightOf="@+id/txtStartTime" />

            <TextView
                android:id="@+id/txtSongTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/sBar"
                android:layout_toRightOf="@+id/btnForward"
                android:text="0 min, 0 sec " />
        </RelativeLayout>


    </android.support.design.widget.NavigationView>

    </android.support.design.widget.BottomNavigationView>

</android.support.design.widget.CoordinatorLayout>

我尝试了几种解决方案,但都没有奏效。我能找到的唯一解决方案是使用 marginbottom,但我认为那将是糟糕的选择。

任何帮助都会很棒。谢谢。

【问题讨论】:

  • 主要问题是您不知道BottomNavigationView 的确切高度是多少,因为它可能会根据底层内容和图像的高度而有所不同。

标签: java android android-layout navigation android-viewpager


【解决方案1】:

您可以通过将ViewPagerBottomNavigationView 包装在ConstraintLayout 中来解决此问题,并添加一个约束,使ViewPager 的底部应该位于BottomNavigationView 的顶部,使用: app:layout_constraintBottom_toTopOf 属性位于ViewPager

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        ...
        app:layout_constraintBottom_toTopOf="@id/bottom_nav_view"

然后使ViewPager的高度匹配约束而不是fill_parentmatch_parent

要应用此更改您的布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- AppBar Layout   -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        <!-- Tab Layout for creating tabs -->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>
    <!-- Helps handing the Fragments for each Tab -->


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toTopOf="@id/bottom_nav_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_nav_view"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:layout_marginTop="5dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <android.support.design.widget.NavigationView

                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_gravity="center"
                    android:orientation="horizontal"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp">

                    <ImageButton
                        android:id="@+id/btnBackward"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_marginLeft="50dp"
                        android:layout_marginBottom="4dp"
                        android:src="@android:drawable/ic_media_rew" />

                    <ImageButton
                        android:id="@+id/btnPlay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/btnBackward"
                        android:layout_marginLeft="20dp"
                        android:layout_toRightOf="@+id/btnBackward"
                        android:src="@android:drawable/ic_media_play" />

                    <ImageButton
                        android:id="@+id/btnPause"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/btnPlay"
                        android:layout_marginLeft="20dp"
                        android:layout_toRightOf="@+id/btnPlay"
                        android:src="@android:drawable/ic_media_pause" />

                    <ImageButton
                        android:id="@+id/btnForward"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/btnPause"
                        android:layout_marginLeft="20dp"
                        android:layout_toRightOf="@+id/btnPause"
                        android:contentDescription="@+id/imageButton3"
                        android:src="@android:drawable/ic_media_ff" />

                    <TextView
                        android:id="@+id/txtStartTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/sBar"
                        android:text="0 min, 0 sec" />

                    <SeekBar
                        android:id="@+id/sBar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/btnBackward"
                        android:layout_toLeftOf="@+id/txtSongTime"
                        android:layout_toRightOf="@+id/txtStartTime" />

                    <TextView
                        android:id="@+id/txtSongTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/sBar"
                        android:layout_toRightOf="@+id/btnForward"
                        android:text="0 min, 0 sec " />
                </RelativeLayout>


            </android.support.design.widget.NavigationView>

        </android.support.design.widget.BottomNavigationView>

    </android.support.constraint.ConstraintLayout>


</android.support.design.widget.CoordinatorLayout>

编辑: 这有点奏效。底部是固定的机器人,现在我看不到顶部,appBar 下的前 2 3 项。我尝试了几个类似app:layout_constraintBottom_toBottomOf="@id/appBarLayout" 的代码,但没有成功。

解决方案: app:layout_constraintTop_toBottomOf="@+id/appBarLayout" 和 gradle 'com.android.support.constraint:constraint-layout:1.0.2' 现在可以正常工作了。

【讨论】:

  • 嘿@Zain 谢谢你的时间。这有点奏效。底部是固定的机器人,现在我看不到顶部,appBar 下的前 2 3 项。我尝试了几个类似app:layout_constraintBottom_toBottomOf="@id/appBarLayout" 的代码,但没有奏效。如果你给我另一个想法,我会很高兴的。顺便说一句,我检查这个答案是真的。
  • 好的,其他任何人都需要它,我添加了app:layout_constraintTop_toBottomOf="@+id/appBarLayout" 和 gradle 'com.android.support.constraint:constraint-layout:1.0.2' 现在它正在工作。再次感谢@Zain
  • @JaneS。感谢您提供新信息,我会将其添加到答案中以帮助遇到相同问题的任何人
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2023-02-10
  • 2023-02-21
  • 2020-10-20
相关资源
最近更新 更多