Android中在使用FragmentManeger添加Frag
参考自 http://www.curious-creature.com/2010/08/15/scrollviews-handy-trick/
今天遇到了一个填充Fragment时不能填充满父控件的问题
明明Fragment的height已经设为match_parent,但填充后就和wrap_content一样
容器的布局
<ScrollView
android:id="@+id/scrollview"
android:layout_marginTop="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:scrollbars="none"
android:layout_above="@+id/navigation">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
fragment的布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
参考stackoverflow大神的说法,在ScrollView中添加 android:fillViewport="true"
问题解决!
这是作者的理解
To understand this result, you must remember that android:layout_height=”fill_parent” means “set the height to the height of the parent.” This is obviously not what you want when using a ScrollView. After all, the ScrollView would become useless if its content was always as tall as itself. To work around this, you need to use the ScrollView attribute called android:fillViewport. When set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.
The XML I wrote to create the correct version of this example can be found below. In line 32, I’ve set the android:layout_weight of the TextView to 1.0. By doing so I am forcing the text to use the available empty space when it is shorter than the ScrollView. This can only work when android:fillViewport=”true” is set on the scroll view.
再来看看文档上是怎么介绍这个api的:
链接需要科学上网:https://developer.android.com/reference/android/widget/ScrollView.html#setFillViewport(boolean)
看到这总结一下,主要是自己对api掌握的不够熟练,还是要学习一个。