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中利用apply、tapply、lapply、sapply、mapply、table等函数进行分组统计
apply函数(对一个数组按行或者按列进行计算): 使用格式为: apply(X, MARGIN, FUN, ...) 其中X为一个数组:MARGIN为一个向量(表示要将函数FUN应用到X的行还是列) ...
- 自己写的jQuery 左右选择框,大家多多指教!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- android 编译 app
有些编写的app需要放到android的源码中进行编译.放置的路径packages/apps/ 编译方法,参考 http://blog.csdn.net/luoshengyang/article/de ...
- Spring 4 官方文档学习(九)数据访问之事务管理
说明:未整理版,未完待续,请绕行 本部分的重点是数据访问以及数据访问层与业务层之间的交互. 1.Spring框架的事务管理 介绍 http://docs.spring.io/spring/docs/c ...
- e646. 处理鼠标点击事件
component.addMouseListener(new MyMouseListener()); public class MyMouseListener extends MouseAdapter ...
- linux -- ubuntu 安装apache后,修改默认路径
默认由apt方式安装的Apache,指定网页主目录位于:/var/www 而系统/var所分到的空间太少,所以要换路径 1.修改默认网站目录 ubuntu8.10下修改apache2的默认文档目录 默 ...
- 【MYSQL】导入中文后乱码问题
http://fatkun.com/2011/05/mysql-alter-charset.html
- 基于pyteseract google ocr的图形验证码识别
先灰化图片,把图片二值化,利用pytesseract包的pytesseract.image_to_string转换出文字.
- Sublime Text2安装Package Control
一.安装Package Control 使用Sublime Text2首先就要安装Package Control,这样就能使用丰富的插件包了 安装方法有2种: (1)访问Package Control ...