From:http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/

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

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

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
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

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);
}
}

Android 让GridView的高度为Wrap_content根据内容自适应高度的更多相关文章

  1. Jquery实现textarea根据文本内容自适应高度

    本文给大家分享的是Jquery实现textarea根据文本内容自适应高度,这些在平时的项目中挺实用的,所以抽空封装了一个文本框根据输入内容自适应高度的插件,这里推荐给小伙伴们. autoTextare ...

  2. html5 textarea 文本框根据输入内容自适应高度

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. 高度随每片内容的高度变化的swiper react-native-unfixed-height-swiper

    高度随每片内容的高度变化的swiper    react-native-unfixed-height-swiper 内容可以文本 图片 视频 本例里面的为文本 使用方式1. npm i react-n ...

  4. IOS UILabel 根据内容自适应高度

    iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelVie ...

  5. iframe内容自适应高度

    一直觉得要用JS才能实现iframe高度的自适应,其实CSS也可以,而且实现的更好,只是需要给包裹iframe的DIV设置个高度,然后让irame高度设置成100%就可以自适应了. 完美版Iframe ...

  6. iframe 随内容自适应高度

    兼容性好的 html代码: <iframe src="enterprise/enter_edit.aspx" id="mainframe" framebo ...

  7. ios label根据内容自适应高度

    label自适应高度,想必大家也都很熟悉怎么去做,上代码: UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(150, 50, 15 ...

  8. MiniUi遇到的一个Bug或者说坑,以div里面的内容自适应高度

    页面源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  9. RecyclerView 高度不能随着Item数量 自适应高度

    在最近项目中遇到 ,在RecyclerView加载list数据时,高度无法自适应增长,看了很多博客,各种尝试,都没有解决这个问题,在某个博客中,讲到此解决方法,在此记录下. 即在RecyclerVie ...

随机推荐

  1. android 监听Home键

    /** * Home 键监听,当按下Home键时,系统会发出action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的BroadcastReceiver * 在程序里动态注册 ...

  2. 【转】WCF入门教程六[一个简单的Demo]

    一.前言 前面的几个章节介绍了很多理论基础,如:什么是WCF.WCF中的A.B.C.WCF的传输模式.本文从零开始和大家一起写一个小的WCF应用程序Demo. 大多框架的学习都是从增.删.改.查开始来 ...

  3. C#的HttpWebRequest编程,支持带ViewState的网页POST请求

    staticprivatestring SearchURL(string id) { try { //Get the ViewState and EventValidation HttpWebRequ ...

  4. SQL Server 备份和还原数据库

    备份: --完整备份 ) set @db_name = 'WSS_Content_Test'; ) set @db_location = 'D:\spbr0002\0000000B.bak'; --保 ...

  5. mysql中,root用户密码被遗忘,该如何进行重置?

    需求描述: 在mysql的测试环境中,有时候会遇到一段时间之后root用户的密码被遗忘的情况, 这个时候,就是需要对root密码进行重置,不过,在生产环境中,这种情况还是很少见. 环境描述: 操作系统 ...

  6. 将Spring源代码导入eclipse步骤

    深入学习spring.研读源代码是必须的~ 1.到https://github.com/spring-projects/spring-framework/releases去找自己须要的spring版本 ...

  7. easyui------设置datagrid('getEditor')时焦点问题

    代码: var dg = $(DataGrid.TableGridID); var row = { ID: "", UserName: "", Password ...

  8. JBPM4.4_jBPM4.4的流程定义语言(设计流程)

    1. jBPM4.4的流程定义语言(设计流程) 1.1. process(流程) 是.jpdl.xml的根元素,可以指定的属性有: 属性名 作用说明 name 流程定义的名称,用于显示. key 流程 ...

  9. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  10. RIDE指定log和report的输出目录

    在命令行中,输入 pybot --help就可以看到他支持的所以命令和相关的介绍 我们可以看到outputdir这个命令,就是来知道report和log的输出目录的 如果你是在命令行中,那么直接后面跟 ...