我正在使用按钮动态加载 gridview。因此,为此我使用了滚动视图,但是如果我将 wrap_content 作为高度分配给 gridview,则不会显示所有按钮。我不想为 gridview 分配任何静态高度。这是我正在使用的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip" >
<GridView
android:id="@+id/gridviewtable"
android:layout_width="fill_parent"
android:layout_height=wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
例如,您需要创建一个新类
public class WrappingGridView extends GridView {
public WrappingGridView(Context context) {
super(context);
}
public WrappingGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WrappingGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec = heightMeasureSpec;
if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
// The great Android "hackatlon", the love, the magic.
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要更改您的 XML
<com.your.project.WrappingGridView
android:id="@+id/gridviewtable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:verticalSpacing="10dp"/>
Run Code Online (Sandbox Code Playgroud)
最后在你的 java 类中,你需要将对象 GridView 设置为 WrappingGridView
FD_*_*FD_ -1
android:layout_height="wrap_content"不能用于AdapterView(例如ListView 和GridView)的子类。GridView 必须获取每行的高度才能计算其自身的高度。
删除ScrollView和LinearLayout,您不需要它们。GridView 已经内置了自己的滚动逻辑。
| 归档时间: |
|
| 查看次数: |
7952 次 |
| 最近记录: |