【发布时间】:2019-02-13 22:51:42
【问题描述】:
如何将 XML 中设置的 ImageButton 的宽度设为 0,并根据权重获得大小?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/fluido"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:tint="@color/icon"
android:layout_weight="0.33"
app:srcCompat="@drawable/fluido" />
<......../>
</LinearLayout>
如何获得这个由权重自动设置的值?
在 XML 中设置为 0,如果我这样做:imageButton.getWidth(); 或 imageButton.getLayoutParams().width(); 都返回 0,就像在 XML 中一样。
【问题讨论】:
-
使用
imageButton.getWidth();,但在布局阶段之后 - 你可以使用ViewTreeObserver -
@pskink 我没听懂,你能解释清楚吗?
-
那么看
ViewTreeObserver官方文档还有什么不清楚的地方?
标签: java android parameters width