我有一个FrameLayout
,可以通过点按Fragments
中的标签来加载TabWidget
。我无法弄清楚如何使FrameLayout
的高度与其内容一样高,以便包含ScrollView
的整个内容将作为一个而不是单独的滚动视图一起滚动。
以下是此Fragment
结构的可视示例:
正如您所看到的,帧布局可见高度仅显示片段的一行,而实际上只有一行。我可以在FrameLayout
内滚动查看现在的其他行,但这不是我想要的。 FrameLayout
由LinearLayout
GridView
组成,其中layout_height
的{{1}}设置为wrap_content
。
我尝试将FrameLayout
的高度硬编码为类似于500dp的东西,除了它不再动态调整大小外,它的效果很好。每次将新图像加载到内部内容中时,是否需要以编程方式调整FrameLayout
的大小?是否有我可以设置的布局属性,因此它会拉伸其高度以匹配其内部内容?
这是我的布局xml文件:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp">
<!-- CONTAINS USER INFO AND STATS -->
</RelativeLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="2">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</ScrollView>
谢谢!
由于我要为此设置赏金,我想我会分享到目前为止我已经想到的东西。
在缩略图中,onSuccess
加载每张图片时,我正在调用GridLayout
中的一个函数,该函数包含计算图像的图像并设置GridLayout
的高度。这很好用,虽然看起来效率有点低。
我正在做的是设置GridLayout
高度,然后在其上调用requestLayout
和invalidate
及其父级。这有效,但不是图像加载。奇怪的是,如果我去另一个标签并返回缩略图,它会起作用。这让我觉得我没有在正确的时间或正确的对象上进行更新。
无论如何,那说。有谁知道如何使GridLayout
展开的高度保持其内容(而不是滚动),以便我可以滚动整个页面(包括顶部)?
我还应该添加GridView
布局:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="false"
android:numColumns="3"
android:choiceMode="none">
</GridView>
答案 0 :(得分:4)
我处于类似情况但我有一个ListView而不是GridView。当您每次添加项目或调用notifyDataSetChanged()时必须动态设置高度时,您就在该部分中。
此代码适用于每行的不同高度的LISTVIEW
private void setListViewHeightBasedOnChildren(MyQueueAdapter listAdapter) {
int desiredWidth = MeasureSpec.makeMeasureSpec(
mListView.getWidth(), MeasureSpec.AT_MOST);
int totalHeight = 0;
View view = null;
int i;
for (i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, mListView);
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mListView.getLayoutParams();
params.height = heightList
+ (mListView.getDividerHeight() * (listAdapter
.getCount() + 3));
heightListComplete = params.height;
mListView.setLayoutParams(params);
}
您需要根据需要修改此代码,因为在您的情况下每行的高度是静态的,所以您不需要循环。如果您需要更多帮助,请告诉我。
<强> ALTERNATIVE 强>
如果您知道dp中视图的高度,您可以轻松转换px中的dp,并根据行数设置gridview的高度。
答案 1 :(得分:2)
使用动态尺寸后,一旦将match_parent
放入wrap_content
内容,就会遇到问题。一个人试图获得一个小的内容,而内容试图变得像它的父母一样大。任何一方最终都不会知道如何正确扩展。
ScrollView
属于这一类。它是内容的可缩放窗口,因此它不能是wrap_content
,内容不能是match_parent
,因为它的父级是虚拟的无限空间。
<ScrollView android:layout_height="wrap_content"
更改为match_parent
(或固定大小)。解决内容的大小
LinearLayout
的根布局(ScrollView
)设置为固定大小,以便其内容可以再次为match_parent
。wrap_content
。wrap_content
,直到孩子定义绝对大小,然后在那里match_parent
。 wrap_content
路径仅在布局中从内到外的所有元素根据其内容正确扩展时才有效。除非你添加一些内容,否则什么都不能依赖于父界限。
您的内容看起来相当动态。因此,您可能需要使用某些代码根据内容手动设置大小。例如。如果您的标签框内的图片是GridView
(基本上ScrollView
再次包含网格内容),您需要手动设置它的大小。包装动态调整大小容器的自由度超过1自动无法解决。
答案 2 :(得分:-1)
框架布局的父级是线性布局,其高度为wrap_content。另外,你的framelayout的高度是wrap_content。将它们都更改为fill_parent。现在使用match_parent是fill_parent
的日子