自定义GridView宽高设置问题,
2014-10-15 10:40
267 查看
当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"
android:background="#eee"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.viewtest.RoomUserGridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ffffff"
android:numColumns="4" />
</LinearLayout>
</ScrollView>这个是RoomUserGridView:
gridView = (RoomUserGridView) findViewById(R.id.gridView);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("name", "dss");
map.put("test", "dss1");
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
gridView.setAdapter(new SimpleAdapter(MainActivity.this, data,
android.R.layout.simple_list_item_2, new String[] { "name",
"test" }, new int[] { android.R.id.text1,
android.R.id.text2 }));
这是运行结果,有两行gridview高度就只有一行:
所以设置gridView的高度,在MainActiviy,gridview.setAdapter后面添加以下代码:
下面是运行结果:
<?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"
android:background="#eee"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.viewtest.RoomUserGridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ffffff"
android:numColumns="4" />
</LinearLayout>
</ScrollView>这个是RoomUserGridView:
public class RoomUserGridView extends GridView { int childHeight = 0; public RoomUserGridView(Context context) { super(context); } public RoomUserGridView(Context context, AttributeSet attributeSet) { super(context, attributeSet); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { System.out.println("onMeasure " + widthMeasureSpec + " " + heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { System.out.println("onLayout " + changed + " " + l + " " + t + " " + r + " " + b); childHeight = b - t; super.onLayout(changed, l, t, r, b); } }MainActivity中gridView适配的时候:
gridView = (RoomUserGridView) findViewById(R.id.gridView);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("name", "dss");
map.put("test", "dss1");
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
data.add(map);
gridView.setAdapter(new SimpleAdapter(MainActivity.this, data,
android.R.layout.simple_list_item_2, new String[] { "name",
"test" }, new int[] { android.R.id.text1,
android.R.id.text2 }));
这是运行结果,有两行gridview高度就只有一行:
所以设置gridView的高度,在MainActiviy,gridview.setAdapter后面添加以下代码:
LinearLayout.LayoutParams p = (android.widget.LinearLayout.LayoutParams) gridView .getLayoutParams(); int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); gridView.measure(w, h); int height = gridView.getMeasuredHeight(); p.height = height * getLine(gridView.getCount()); gridView.setLayoutParams(p);<pre name="code" class="java">public int getLine(int count) { if (count % 4 == 0) { return count / 4; } else { return count / 4 + 1; } }
下面是运行结果:
相关文章推荐
- GridView动态设置条目宽高,第一个条目显示不清晰或不完整的问题
- 关于DataGrid以及GridView属性设置的问题
- ASP.NET2.0 GridView控件两大常见问题解决方案:自定义数据分页,类似DataGrid的VirtualItemCount功能
- DevExpress.XtraGrid.GridView 自定义样式设置
- GridView内边框设置的问题
- 关于GridView设置编辑或不可编辑的相关问题!
- [转帖]GridView的时间格式设置问题
- Java中JDK中path和classpath的设置相关问题(关于import自定义包和jar的问题)
- 方正中间件平台设置表单中自定义样式问题
- 方正中间件平台设置表单中自定义样式问题
- 解决gridview禁止换行设置无效的问题
- 一个简单的GridView设置复合表头问题。
- GridView 自定义分页--DropDownList翻页与保存状态问题
- GridView内边框设置的问题
- 提醒:ClientIDMode如果设置成Static,GridView如果放在UpdatePanel中,就会产生刷新的问题!
- 复杂自定义数据对象绑定到GridView的问题
- GridView绑定自定义数据源(list)排序问题
- SharePoint : 自定义权限设置中可能遇到的问题
- UIColor设置自定义的颜色不成功问题
- Android自定义布局的背景在多分辨率的情况下设置fill_parent时背景不能够横向全屏的问题解决