如果设置layout_height = wrap_content,则ViewPager无法正常工作
使用 ViewPaper
时遇到问题.设置ViewPaper的 height = wrap_content
时,它什么都没有显示,并且显示了是否为 ViewPager
设置了高度(例如: android:layout_height ="350dp"
).请帮忙!谢谢!
I got issues when worked with ViewPaper
.
It doesn't show anything when I set ViewPaper's height = wrap_content
and it shows if I set height for ViewPager
(ex: android:layout_height= "350dp"
). Please help!
Thanks!
这是我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FF0000"
android:gravity="center"
android:text="Quick Links"
android:textColor="@color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circlePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pager"
android:layout_marginBottom="10dp" >
</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>
<com.custom.ExpandableHeightListView
android:id="@+id/lvAdmissions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:divider="@drawable/bg_listview_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector_animation" >
</com.custom.ExpandableHeightListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
已解决:我点击了此链接,它可以帮助我解决此问题. https://stackoverflow.com/a/24666987/1928560
Resolved: I follow this link and it help me resolved this issues. https://stackoverflow.com/a/24666987/1928560
ViewPager高度必须指定为 match_parent
或必须为其指定一个值.两种解决方案,
ViewPager height must either be specified match_parent
or you must specify a value for it. Two solutions,
- 在
LinearLayout
中使用 weights 概念或 - 在类文件中使用
onMeasure
方法.请参考 Android:我无法安装ViewPager WRAP_CONTENT
- Use weights concept inside
LinearLayout
or - Use
onMeasure
method in your class file. Refer Android: I am unable to have ViewPager WRAP_CONTENT
这是在 LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FF0000"
android:gravity="center"
android:text="Quick Links"
android:textColor="@color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circlePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pager"
android:layout_marginBottom="10dp" >
</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>
<com.custom.ExpandableHeightListView
android:id="@+id/lvAdmissions"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
android:divider="@drawable/bg_listview_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector_animation" >
</com.custom.ExpandableHeightListView>
</LinearLayout>
这样,您的viewpager布局和ExpandableListView共享相同的高度.您可以通过更改重量值来更改比例
This way your viewpager layout and ExpandableListView shares equal height. You can change the ratio by changing the weight value