Android 让GridView的高度为Wrap_content根据内容自适应高度
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根据内容自适应高度的更多相关文章
- Jquery实现textarea根据文本内容自适应高度
本文给大家分享的是Jquery实现textarea根据文本内容自适应高度,这些在平时的项目中挺实用的,所以抽空封装了一个文本框根据输入内容自适应高度的插件,这里推荐给小伙伴们. autoTextare ...
- html5 textarea 文本框根据输入内容自适应高度
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 高度随每片内容的高度变化的swiper react-native-unfixed-height-swiper
高度随每片内容的高度变化的swiper react-native-unfixed-height-swiper 内容可以文本 图片 视频 本例里面的为文本 使用方式1. npm i react-n ...
- IOS UILabel 根据内容自适应高度
iOS Label 自适应高度 适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelVie ...
- iframe内容自适应高度
一直觉得要用JS才能实现iframe高度的自适应,其实CSS也可以,而且实现的更好,只是需要给包裹iframe的DIV设置个高度,然后让irame高度设置成100%就可以自适应了. 完美版Iframe ...
- iframe 随内容自适应高度
兼容性好的 html代码: <iframe src="enterprise/enter_edit.aspx" id="mainframe" framebo ...
- ios label根据内容自适应高度
label自适应高度,想必大家也都很熟悉怎么去做,上代码: UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(150, 50, 15 ...
- MiniUi遇到的一个Bug或者说坑,以div里面的内容自适应高度
页面源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- RecyclerView 高度不能随着Item数量 自适应高度
在最近项目中遇到 ,在RecyclerView加载list数据时,高度无法自适应增长,看了很多博客,各种尝试,都没有解决这个问题,在某个博客中,讲到此解决方法,在此记录下. 即在RecyclerVie ...
随机推荐
- 用R作Polar图等
用R作如下的各国Gini系数的Polar barChart: 作上图的R代码为: library(ggplot2) GiniData<- read.csv('IncomeInequality.c ...
- android DVM
android 运行环境主要是虚拟机技术:Dalvik.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的 Dalvik虚拟机实例.而每一个DVM都是在Linux中的一个进程,所以说 ...
- thinkphp 点击分类显示分类下的文章(完整)
控制器 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function index(){ $cate=M ...
- 关于用Spine制作骨骼动画的步骤
步骤: 1.打开spine. 2.新建一个空项目. 3.点右边Tree下的Images模块,导入在自己的切片素材和原整图. 4.把原整图拖到场景中,点右下角Color改变原图的颜色,方便后面对照. 5 ...
- Ubuntu之Docker安装
1.添加官方的GPG key到系统 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 2.添加D ...
- Tensorflow参数初始化很慢的问题
首先查看是否使用了import cv2 如果有import cv2,说明是opencv的问题 因为如果你的opencv是本地编译的,那么很可能使用了cudnn进行编译,那么这个cv2就会占用显存,并且 ...
- imx6 hdmi接口支持
/************************************************************* * imx6 hdmi接口支持 * 新的板子需要使用到hdmi,今天就测试 ...
- caffe源代码分析--Blob类代码研究
作者:linger 转自须注明转自:http://blog.csdn.net/lingerlanlan/article/details/24379689 数据成员 shared_ptr<Sync ...
- vs2013+MVC3.0+EasyUI的ComboBox联动使用(二)
vs2013+MVC3.0+EasyUI的ComboBox联动使用(二) 简单介绍:在vs2013(.net4.0)中使用MVC3.0对于EasyUI中ComboBox的联动使用. 载入Comb ...
- HDU 1556 Color the ball 树状数组 题解
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...