【发布时间】:2015-04-08 23:57:13
【问题描述】:
如何通过自动调整其高度来防止 gridView 滚动?我希望所有项目,无论我向 gridView 添加多少项目,都可以在不滚动的情况下保留在屏幕上。这可能吗?
这是我目前的用户界面。
<?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="@drawable/bg"
android:orientation="vertical">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv_header"
android:fadingEdge="none"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
我确实尝试将 weightSum 添加到 root 并将 weight 添加到 gridView,但它仍然需要滚动。
更新:我也尝试使用自定义网格视图。这不起作用,但无论如何这是我的尝试。
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST));
getLayoutParams().height = getMeasuredHeight();
}
}
提前致谢!
【问题讨论】:
-
您希望网格项目缩小得越多,以便整个网格在屏幕上可见而无需滚动?
-
@krislarson 是的!这就是我想要达到的目标。有什么想法吗?
-
这真的取决于您使用的项目视图。我认为您需要两部分,1)您需要从适配器中获取 getCount() ,然后使用适合您所有项目的列数调用 gridview.setNumColumns() 。 2)项目需要有一个onMeasure(),它将根据宽度限制高度,例如带有setAdjustViewBounds(true)的ImageViews。同样,如果不确切知道您要显示的内容,就很难说。