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 ...
随机推荐
- cause: java.lang.IllegalStateException: Serialized class com.taotao.pojo.TbItem must implement java.io.Serializable
HTTP Status 500 - Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: ...
- 当数据库的字段为date类型时候
当数据库的字段为date类型时候: //--------------------------------------------------数据库设计------------------------- ...
- 为iframe添加鼠标事件
1.关于iframe标签 使用iframe元素会创建包含另外一个文档的内联框架(即行内框架).所以我们可以使用iframe标签,在一个页面嵌入另一个页面.通过指定iframe的src为另一个页面的路径 ...
- jquery实现简单瀑布流代码
测试环境:ie8 ff13.0.1 chrome22 可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度 <!DOCTYP ...
- Linux中显示一个文件最后几行的命令
tail -n 20 filename说明:显示filename最后20行. Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端 ...
- Hibernate中createCriteria即QBC查询的详细用法
现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = se ...
- 【转】WCF入门教程二[WCF应用的通信过程]
一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...
- 快速找出System.Management.Automation.dll,c#调用powershell
public static void InvokeSystemPS(string cmd) { List<string> ps = new List<string>(); ps ...
- bat、cmd、dos窗口:后台调用,不显示黑色的控制台dos(命令行)窗口
建立一个windows的vbs脚本文件,内容类似如下:注意末尾的参数0 createobject("wscript.shell").run "VBoxheadless.e ...
- liunx下误删除/var目录下的empty文件,导致ssh连接不上
清理Liunx上不用的文件,导致误删 /var/下的empty文件,因为用的是ftp删的,所以可能有隐藏文件没有看到,导致其他同事都登录不上去 解决方法: 1.在/var文件夹下,重新建立empty文 ...