gpt4 book ai didi

android - 如何为 ImageView 设置LayoutParams()?

转载 作者:IT老高 更新时间:2023-10-28 13:13:20 26 4
gpt4 key购买 nike

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

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

此代码不起作用...

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

我该怎么做?

最佳答案

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

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

这是因为 View 的父级需要知道分配给 View 的大小。

关于android - 如何为 ImageView 设置LayoutParams()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com