【问题标题】:ViewPager in RelativeLayout consumes full Height (Tabs)RelativeLayout 中的 ViewPager 消耗完整的高度(选项卡)
【发布时间】:2018-03-29 20:27:42
【问题描述】:

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

为了实现以下设计: sketch

我用RelativeLayout 包围了ViewPager,以便在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>

非常感谢!

【问题讨论】:

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


    【解决方案1】:

    更改这部分代码:

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

    【讨论】:

    • 谢谢兄弟 :) 我在元素中添加了重力属性 android:layout_weight=".6" 和 android:layout_weight=".4" 。这正是我想要的..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2013-09-26
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多