在Android中,可以使用以下方法來獲取控件的寬高:
View view = findViewById(R.id.your_view_id);
int width = view.getWidth();
int height = view.getHeight();
View view = findViewById(R.id.your_view_id);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int width = view.getWidth();
int height = view.getHeight();
// do something with width and height
}
});
通過以上兩種方法,就可以在Android中獲取到控件的寬高值。