GridView 设置其高度属性为 wrap_content

本文介绍如何在Android中使用自定义的GridView组件,使其高度能够适应所有内容,而不是仅显示一行内容。通过覆盖GridView的onMeasure函数,可以实现在垂直滚动布局中根据内容动态调整GridView的高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果把GridView放到一个垂直方向滚动的布局中,设置其高度属性为 wrap_content ,则该GridView的高度只有一行内容,其他内容通过滚动来显示。 如果你想让该GridView的高度为所有行内容所占用的实际高度,则可以通过覆写GridView的 onMeasure 函数来修改布局参数:
Java代码


package com.jayway.components;
 
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
 
public class MyGridView extends GridView {
     public MyGridView(Context context) {
         super (context);
     }
 
     public MyGridView(Context context, AttributeSet attrs) {
         super (context, attrs);
     }
 
     public MyGridView(Context context, AttributeSet attrs, int defStyle) {
         super (context, attrs, defStyle);
     }
 
     @Override
     protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
         int heightSpec;
 
         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);
         }
         else {
             // Any other height should be respected as is.
             heightSpec = heightMeasureSpec;
         }
 
         super .onMeasure(widthMeasureSpec, heightSpec);
     }
}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<? xml version = "1.0" encoding = "utf-8" ?>
 
< ScrollView
     android:layout_width = "match_parent"
     android:layout_height = "match_parent" >
 
     < LinearLayout
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:orientation = "vertical" >
 
         <!-- Header One -->
     < TextView
             android:layout_width = "match_parent"
             android:layout_height = "wrap_content"
             android:focusable = "true"
             android:text = "Header 1" />
 
         <!-- Custom grid view holding the 'Group 1' items -->
         < com.jayway.components.MyGridView
             android:layout_width = "match_parent"
             android:layout_height = "wrap_content"
             android:numColumns = "3"
             android:stretchMode = "columnWidth"
             android:id = "@+id/grid_view_1" />
 
         <!-- Header 2 -->
         < TextView
             android:layout_width = "match_parent"
             android:layout_height = "wrap_content"
             android:text = "Header 2" />
 
         <!-- Custom grid view holding the 'Group 2' items -->
         < com.jayway.components.MyGridView
             android:layout_width = "match_parent"
             android:layout_height = "wrap_content"
             android:numColumns = "3"
             android:stretchMode = "columnWidth"
             android:id = "@+id/grid_view_2" />
     </ LinearLayout >
</ ScrollView >

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.jayway.app;
 
import java.util.ArrayList;
 
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.app.Activity;
 
import com.jayway.components.MyGridView;
 
public class MainActivity extends Activity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         ArrayList labels = new ArrayList();
         for ( int i = 0 ; i < 24 ; i++) {
             labels.add( "Item " + i);
         }
 
         ArrayAdapter adapter = new ArrayAdapter(
                 this , android.R.layout.simple_list_item_1, labels);
 
         MyGridView grid1 = (MyGridView) findViewById(R.id.grid_view_1);
         grid1.setAdapter(adapter);
 
         MyGridView grid2 = (MyGridView) findViewById(R.id.grid_view_2);
         grid2.setAdapter(adapter);
     }
}


Read more: http://blog.chengyunfeng.com/?p=444#ixzz3lPTNs9KJ
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context="com.jd.projects.wlw.shuhai.ShuhaiSp"> <!-- <!– 顶部工具栏 –>--> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#44bd32"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="90dp" android:layout_above="@+id/thumbnail_scrollview" android:layout_below="@id/toolbar" android:layout_marginTop="0dp" android:background="@android:color/black"> <!-- <ProgressBar--> <!-- android:id="@+id/video_loading"--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_gravity="center" />--> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="400dp" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="match_parent" android:layout_height="290dp" android:layout_alignParentBottom="true" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:alignmentMode="alignBounds" android:columnCount="2" android:padding="8dp" android:rowCount="0" android:background="#ffffff" android:useDefaultMargins="true"> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> </android.widget.GridLayout> </LinearLayout> </ScrollView> </RelativeLayout>解决布局文件中<ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" />缩略图没有显示在<android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:alignmentMode="alignBounds" android:columnCount="2" android:padding="8dp" android:rowCount="0" android:background="#ffffff" android:useDefaultMargins="true">这一模块中
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值