简体   繁体   中英

Scroll View is not working with relative layout

I tried to make a scroll view. However,it does not display the viewPager inside the scroll view. I don't know which part is wrong.

This is my xml file

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

This is the output 在此处输入图片说明

The viewPager is not displayed. The viewPager will be displayed only when I removed the scroll view

try this out:

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

Try adding this piece of code before loading your viewPager

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

Add

android:fillViewport="true"

to scrollview and set scrollview height to

android:layout_height="300dp"

solve my problem.

However, I dont know why the height cannot set as wrap_content

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