android - 从消失后更改可见性后获取图像宽度
问题描述
ImageView
我在里面有约束布局:
<ImageView
android:id="@+id/iv_lottery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_lottery_info"
tools:visibility="visible" />
在我的片段中,当我收到网络响应时,我会显示ImageView
并尝试从网络加载图像,Picasso
并使用自动高度调整它的大小。
mIvLottery.setVisibility(View.VISIBLE);
Picasso.get().load(mLottery.getBanner()).resize(mIvLottery.getWidth(), 0).into(mIvLottery);
问题是:mIvLottery.getWidth()
是0。
我该如何解决?
如果我将 xml 布局更改android:visibility from
gone
为invisible
or visible
,一切正常。问题仅在默认可见性ImageView
为时gone
。
解决方案
因为mIvLottery
可见性设置GONE
为非常好,宽度为 0。
您在这里有几种解决方案,但不知道您的约束是什么,我想说最快的方法是从mLottery.getBanner()
.
否则,如果您需要计算屏幕上尚未显示的视图的宽度,您可以依赖measure()
API。
所以调用measure()
和getMeasuredWidth()
之后应该给你你正在寻找的宽度。请记住 measure 接受View.MeasureSpec作为参数,因此它取决于父布局选择哪些
mIvLottery.measure(UNSPECIFIED, AT_MOST);
int width = mIvLottery.getMeasuredWidth();
推荐阅读
- javascript - jquery中要求最少6个字符,至少1个大写字母,1个小写字母,1个数字和1个特殊字符
- html - 收缩时弹性项目的位置高于其他弹性项目
- android - 使用 Firebase 身份验证的电子邮件 firebase 无法正常运行?
- c++ - 保存到静态类时 WIFI 名称 (char*) 行为异常
- unity3d - 图层蒙版问题 - 即使图层蒙版已更改,玩家仍会与敌人发生碰撞(Unity、Bolt 脚本)
- python - Heroku 无法识别 Procfile
- arduino - 用arduino控制360度伺服
- javascript - 如何找到执行 eval 代码的 [content] 脚本
- r - R:如何根据 R 中另一行的值扩展一行中的值?
- html - 当html代码不直接包含选项时如何使一个按钮默认单选按钮