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. centos7+ docker1.12 实践部署docker及配置direct_lvm

    前言 Docker现在在后端是那么的火热..尤其当笔者了解了docker是什么.能做什么之后,真的是感觉特别的exciting,便迫不及待的去实践部署一下. 但是在实际部署中,因为笔者使用的是阿里云e ...

  2. Jquery easyui教程

    目  录 1基本拖放.......................................................................................... ...

  3. jQuery Form插件详解

    <script src="js/jquery.form.js" type="text/javascript"></script> Jqu ...

  4. Qt信号槽的一些事

    注:此文是站在Qt5的角度说的,对于Qt4部分是不适用的. 1.先说Qt信号槽的几种连接方式和执行方式. 1)Qt信号槽给出了五种连接方式: Qt::AutoConnection 0 自动连接:默认的 ...

  5. thinkphp 第一个设计总结

    1.thinkphp的无限级分类不是万能的... 2.感觉先看前台(根据前台设计数据库)后写控制代码(后台),速度或许会快一点,思路明确...

  6. 关于Unity中的碰撞检测和管理(2D)

    创建Unity3D项目和Unity2D项目的区别. 1.3D项目有摄像机和太阳光,2D项目只有摄像机 2.3D项目使用的贴图类型是Texture纹理,2D项目使用的贴图类型是Sprite 2D/UI ...

  7. Python调用打印机参考例子

    参考资料: http://blog.csdn.net/jdh99/article/details/42585987 http://www.oschina.net/question/1438043_23 ...

  8. par函数cex参数-控制文字和点的大小

    cex参数用来控制图片中点和文字的大小,对于一副图片来说,有很多的文字部分,包括x轴标签(xlab), y轴标签(ylab), x轴刻度上的文字, y轴刻度上的文字,主标题(main), 副标题(su ...

  9. 关于Unity 获得和使用GetComponent<MeshFilter>().mesh时的心得

    原文地址:http://blog.sina.com.cn/s/blog_7d9405e50100s061.html 今天在使用Unity3D的时候遇到了一个问题:_tesGameObject是在Pro ...

  10. Linux 代理设置

    apt proxy vim /etc/apt/apt.conf Acquire::http::proxy "http://10.48.127.169:8080/"; Acquire ...