你如何为 ImageView 设置布局参数()? [英] How do you setLayoutParams() for an ImageView?

查看:28
本文介绍了你如何为 ImageView 设置布局参数()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 ImageView 设置 LayoutParams 但似乎无法找到正确的方法.

I want to set the LayoutParams for an ImageView but cant seem to find out the proper way to do it.

我只能在 API 中找到各种 ViewGroups 的文档,而不是 ImageView.然而 ImageView 似乎有这个功能.

I can only find documentation in the API for the various ViewGroups, but not an ImageView. Yet the ImageView seems to have this functionality.

此代码不起作用...

myImageView.setLayoutParams(new ImageView.LayoutParams(30,30));

我该怎么做?

推荐答案

您需要设置 ImageView 所在的 ViewGroup 的 LayoutParams.例如,如果您的 ImageView 在一个 LinearLayout 内,那么您创建一个

You need to set the LayoutParams of the ViewGroup the ImageView is sitting in. For example if your ImageView is inside a LinearLayout, then you create a

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

这是因为它的父视图需要知道分配给视图的大小.

This is because it's the parent of the View that needs to know what size to allocate to the View.

这篇关于你如何为 ImageView 设置布局参数()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆