RelativeLayout中的ViewPager消耗完整高度(标签)

时间:2018-03-29 20:27:43

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

我正在使用AndroidStudio 3.0,我正在使用Java创建带有Fragments的TabbedActivity。

为了实现以下设计: sketch

我将ViewPager与RelativeLayout联系起来,以便在ViewPager下面显示静态内容对象(TextView) 。为此,我使用属性

android:layout_below="@id/container"

不幸的是,TextView永远不会显示出来。 通过摸索我意识到,在设置

android:layout_height="100dp"

将ViewPager显示为固定值。

由于我是Android的初学者,我不确定如何处理这个问题。使用ViewPagers时是否无法使用layout_below属性?或者我必须为他们设置固定值?

是否有其他解决方案可以达到我想要的效果 - 只需在'TabControl'下面显示静态内容?

这是我完整的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_weight="1"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name">

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab_text_1" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab_text_2" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tab_text_3" />

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

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


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

            </android.support.v4.view.ViewPager>

            <TextView
                android:id="@+id/static_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="This is the Textiew that is not shown!"
                android:layout_below="@id/container"

            />

        </RelativeLayout>

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

非常感谢!

1 个答案:

答案 0 :(得分:0)

更改代码的这一部分:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        </android.support.v4.view.ViewPager>

        <TextView
            android:id="@+id/static_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="This is the Textiew that is not shown!"
            android:layout_below="@id/container"

        />

    </RelativeLayout>

我会用垂直LinearLayout替换它。这样,您就可以为TextView提供一些空间,然后将剩下的空间分配给ViewPager,而RelativeLayout将不允许这样做。

<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:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>

    <TextView
        android:id="@+id/static_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is the Textiew that is not shown!"/>

</LinearLayout>