gpt4 book ai didi

android - 动态创建时在绘制之前找到textView的宽度

转载 作者:行者123 更新时间:2023-11-29 01:26:44 27 4
gpt4 key购买 nike

我有一个右下角有 imageview1 的 Activity 。 Activity 开始并调用 onWindowFocusChanged 后,我想动态创建一个 textView 并将其放在 imageview1 旁边(左侧)。我知道我可以通过修改其 layoutParam 来设置 textView 的位置,但要正确对齐它,我会执行以下操作

param.setMargins(0, imageView1.getTop(), rootLayout.getWidth()-imageView1.getLeft()-textView.getWidth, 0);

.问题是我无法找出 textView 的宽度,因为它还没有被绘制出来。我该如何解决这个问题?

最佳答案

您可以要求 View 测量自身,但您应该指定约束。例如:

final Button button = new Button(this);
button.setText("Button");
button.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
Log.d("Test", "measured width: " + button.getMeasuredWidth());
Log.d("Test", "measured height: " + button.getMeasuredHeight());

然后你有:

11-09 10:33:14.316 14959-14959/com.madcucumber.testapplication D/Test: measured width: 264
11-09 10:33:14.316 14959-14959/com.madcucumber.testapplication D/Test: measured height: 144

关于android - 动态创建时在绘制之前找到textView的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596570/

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