简体   繁体   中英

ViewPager in RelativeLayout consumes full Height (Tabs)

I am working with AndroidStudio 3.0 and I am creating a TabbedActivity with Fragments using Java.

For realizing the following design: sketch

i sourrounded the ViewPager with a RelativeLayout in order to display a static content object (TextView) below the ViewPager. For this I am using the attribute

android:layout_below="@id/container"

Unfortunately, the TextView never gets shown. By fuzzling around i realized, that when setting the

android:layout_height="100dp"

of the ViewPager to a fixed value it will be displayed.

As I am a beginner to Android I am unsure how to handle this issue. Is it not possible to use the layout_below attribute when working with ViewPagers? Or do i have to set a fixed value for them?

Is there another solution to achieve what i want - simply showing a static content below a 'TabControl'?

Here is my complete XML code:

<?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>

Thank You very much!

Change this part of your code:

  <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> 

I would replace this with a vertical LinearLayout . This allows you to give some space to the TextView and then give the rest of the space to the ViewPager , which a RelativeLayout won't allow.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM