gpt4 book ai didi

java - 为什么 LinearLayout instance.getLayoutParams 看起来有一个错误的类?

转载 作者:搜寻专家 更新时间:2023-10-30 20:58:09 27 4
gpt4 key购买 nike

如果我声明 LinearLayout linearLayout 并查看 linearLayout.getLayoutParams(),它会给我 ViewGroup.LayoutParams,而不是 LinearLayout .LayoutParams.

所以我必须使用重复(因此不好)的样式构造:

int lm = ((LinearLayout.LayoutParams) linearLayout.getLayoutParams()).leftMargin?

例如,如果我想达到利润率,我真的必须使用它吗?

是我对 Android 还是 Java,或者两者都有误解,还是别的?

最佳答案

我认为您对“LayoutParams”的理解不正确。 View (或布局)的 LayoutParams 必须是“父 View 的 LayoutParams”的实例。

例如,这里是 RelativeLayout 中的 LinearLayout。即LinearLayout的LayoutParams必须是RelativeLayout.LayoutParams。这个东西使我们能够正确设置属性,如“centerInParent”等。

当您调用 getLayoutParams() 时,会返回与父 ViewGroup 类型匹配的 LayoutParams 的各种实例。因此,我们需要在调用 getLayoutParams() 时反复转换 View 的 LayoutParams 类型。

关于java - 为什么 LinearLayout instance.getLayoutParams 看起来有一个错误的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8942415/

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