代码之家  ›  专栏  ›  技术社区  ›  blaner0

RelativeLayout中的ViewPager使用全高(选项卡)

  •  1
  • blaner0  · 技术社区  · 7 年前

    我正在使用AndroidStudio 3.0,我正在使用Java创建一个带有片段的TabbedActivity。

    为了实现以下设计: sketch

    为了显示静态内容对象(TextView),我用RelativeLayout标记了ViewPager 在下面 ViewPager。为此,我使用属性

    android:layout_below="@id/container"
    

    不幸的是,文本视图从未显示。 通过模糊观察,我意识到

    android:layout_height="100dp"
    

    将ViewPager的 固定的 将显示该值。

    由于我是Android的初学者,我不确定如何处理这个问题。使用ViewPagers时,是否不能使用layout\u 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 回复  |  直到 7 年前
        1
  •  0
  •   Ben P.    7 年前

    更改代码的这一部分:

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