android 获得组件宽度,Android 获取控件的宽高

Android 获取控件的宽高

Android,layout,布局,view,

在Android开发中,很多时候,在使用XML布局后,还需要在Java代码对内容进行一些修改或者添加之类的操作。

setContentView(layout_id)

View view = findViewById(view_id);

int width = view.getWidth();

int height = view.getHeight();

这样的方式获取到的宽高都会是0.

在这些代码放在 onCreate(),onResume(),onStart() 执行的结果都是一样的。因为在这个时候还没有为这个布局算好宽高。老外是这么说的 The issue here is you are trying to get the parent height/weight before the layoutmanager completes parent layout.

另外一个老外是这么说的 One alternative to programmatically implement this is to use following method:

public void onWindowFocusChanged(boolean b){...}

By the time this method is invoked, the layouting-process has been finished. Thus you can obtain the parent attributes correctly.

这个方法在会在布局线程结束后有个回调。至于这个方法会不会在其他的情况下回调,这个继续跟踪中……

相关文章

- EOF -

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。

转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]

本文标题: Android 获取控件的宽高

本文地址:https://binkery.com/archives/182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值