滚动视图不适用于相对布局

时间:2017-10-03 13:46:17

标签: android android-layout android-scrollview android-relativelayout

我尝试制作滚动视图。但是,它不会在滚动视图中显示viewPager。我不知道哪个部分是错的。

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.qianonnphoon.tradeal.displaytrade.DisplayTradeActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp">
        </android.support.design.widget.TabLayout>

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="23dp"
    android:layout_below="@+id/view_pager"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/tvOwnItemName"/>
</RelativeLayout>
</ScrollView>

这是输出 enter image description here

不显示viewPager。仅当我删除滚动视图

时才会显示viewPager

3 个答案:

答案 0 :(得分:0)

试试这个:

    <ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:scrollbars="vertical">
    android:fillViewport="true" >


    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</RelativeLayout>

</ScrollView>

答案 1 :(得分:0)

尝试在加载viewPager之前添加这段代码

NestedScrollView scrollView = (NestedScrollView) findViewById 
(R.id.nest_scrollview);
scrollView.setFillViewport (true);

答案 2 :(得分:0)

添加

android:fillViewport="true"

滚动查看并将scrollview高度设置为

android:layout_height="300dp"

解决我的问题。

但是,我不知道为什么高度不能设置为wrap_content

相关问题