gpt4 book ai didi

android - 获取实际的 View 高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:52 25 4
gpt4 key购买 nike

简单的问题,也许是令人沮丧的复杂但希望是简单的答案?

获取大小设置为 FILL_PARENT 的元素的实际高度的“最佳实践”是什么?

最佳答案

确实比看起来更复杂。与建议问题中的答案相比,我发现的一种更简单的方法是使用 ViewTreeObserver。在您的 onCreate() 方法中,您可以使用如下代码:

TextView textView = (TextView)findViewById(R.id.my_textview);
ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//in here, place the code that requires you to know the dimensions.
//this will be called as the layout is finished, prior to displaying.
}
});

关于android - 获取实际的 View 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4501840/

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