【发布时间】:2017-08-03 01:54:17
【问题描述】:
我正在开发一个带有头像(图像图标)的 Android 应用,ImageView 位于这样的 RelativeLayout 中
<ImageView
android:id="@+id/indicators"
android:layout_width="@dimen/conversation_avatar_size"
android:layout_height="@dimen/conversation_list_left_indicator_height"
android:layout_below="@id/contact_image"
android:layout_alignWithParentIfMissing="true"
android:layout_marginEnd="@dimen/smaller_def_margin"
android:layout_marginTop="@dimen/smaller_def_margin"
android:scaleType="fitCenter"
tools:src="@drawable/ic_reply"/>
但是,当我使 contact_image 不可见时,我无法更改此 ImageView 的宽度,因为指示器占用的空间更少。代码如下
indicators.getLayoutParams().width = width;
indicatorsWidth = indicators.getWidth();
indicators.requestLayout();
在indicators.requestLayout(); 之后,指示器的宽度仍然保持旧的未更改为宽度。我想知道为什么会发生这种情况?我也试过indicators.setLayoutParams(layoutParams);,但还是不行。
我确实发现了一些有趣的东西,尽管 indicators.getWidth() 仍然是旧值,indicators.getLayoutParams().width 现在是新宽度,并且 ImageView 大小根本没有改变。
更新:我找到view.getWidth()是什么,然后我用indicators.setLeft()和indicators.setRight(),这次indicators.getWidth()也是新的宽度,但是大小还是没变,我查了一下发现@ 987654331@ 仍然是旧值,知道吗?
【问题讨论】:
标签: android xml android-layout android-imageview