gpt4 book ai didi

android - 以编程方式获取约束的 ID

转载 作者:行者123 更新时间:2023-11-29 15:34:41 26 4
gpt4 key购买 nike

给定以下 xml:

<ImageView
android:id="@+id/someTag"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="8dp"
android:tag="someTag"
app:layout_constraintBottom_toTopOf="@+id/someID"
app:layout_constraintStart_toEndOf="@+id/someID"

我想知道如何获取id

app:layout_constraintBottom_toTopOf

(即“someID”)以编程方式。

非常感谢!

最佳答案

该属性值保存在 ImageViewLayoutParams 中,具体为 ConstraintLayout.LayoutParams ,在本例中,因为它的父级是 ConstraintLayout

ConstraintLayout.LayoutParamsbottomToTop字段,其中包含该特定约束的 View 的 ID。

只需获取 ImageViewLayoutParams,将它们转换为 ConstraintLayout.LayoutParams,然后从上述字段中获取您的 ID。例如:

ImageView image = findViewById(R.id.someTag);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) image.getLayoutParams();
int bottomToTopId = lp.bottomToTop;

如果出于某种原因,您需要该 ID 的实际资源名称,您可以照常使用 Resources#getResourceEntryName() 方法获取它。即:

String viewIdName = getResources().getResourceEntryName(bottomToTopId); // returns "someID"

关于android - 以编程方式获取约束的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343965/

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