---
title: "GridView 、Gallery以及普通View中LayoutParams的设置"
author: "mmdev"
date: "2012-12-11T15:42:00Z"
tags: []
slug: "gridview-gallery-view-layoutparams"
draft: false
lastmod: "2013-01-05T09:53:22Z"
id: 1761152
url: "https://mmdev.iteye.com/blog/1761152"
---

以下理解均为错误，因为LayoutParams是属于ViewGroup的，对于一个ViewGroup中的子元素，可以用该ViewGroup的LayoutParams

GridView和Gallery中都有LayoutParams为他们的子View设置参数

```
  ImageView imageView=new ImageView(mContext);
  imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
  imageView.setImageResource(mImageIds[position]);
```

关于普通View设置参数的办法为

```
   LayoutParams params = viewgroup.getLayoutParams();
  params.height=50;
  params.width=50;
  viewgroup.setLayoutParams(params);
以上的LayoutParams是引自ViewGroup的

以上理解均为错误，因为LayoutParams是属于ViewGroup的，对于一个ViewGroup中的子元素，可以用该ViewGroup的LayoutParams


```