gpt4 book ai didi

android - RelativeLayout 中 XML 文件中 View 的 getTop()/getLeft 不起作用

转载 作者:行者123 更新时间:2023-11-29 18:14:24 25 4
gpt4 key购买 nike

我的布局:

<RelativeLayout...>
<Button
android:id="@+id/button_1"
...
/>
<Button
android:id="@+id/button_2"
android:layout_toRightOf="@+id/button_1"
...
/>
...
</RelativeLayout>

我想保存所有按钮的 x 和 y 位置。

我试过这个:

Button b[] = new Button[40];
int parX [] = new int[40];
int parY [] = new int[40];
String temp;
int resID;

for (int i=0;i<=39;i++)
{
temp = "id/button_" + String.valueOf(i+1);
resID = getResources().getIdentifier("com.myapp.test:"+temp, null, null);
b[i] = (Button) findViewById(resID);
parX[i] = b[i].getTop();
parY[i] = b[i].getLeft();
}

但是 b[i].getTop() 和 b[i].getLeft() 总是 0。但它不可能是因为它被正确地绘制了一个字段40 个按钮,因此它们必须具有所有不同的 x 和 y 位置。

如何找出按钮的 x/y 位置我的 xml 文件?

最佳答案

getLeft/getTop 只会在第一次布局通过后返回正确的值。

关于android - RelativeLayout 中 XML 文件中 View 的 getTop()/getLeft 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8946704/

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