获得布局的高度和宽度在运行时的android [英] get layout height and width at run time android
本文介绍了获得布局的高度和宽度在运行时的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能得到它在xml无论是在高度和宽度定义为FILL_PARENT线性布局的宽度和高度?我曾尝试onmeasure方法,但我不知道为什么它没有给予确切的价值。我在活动中需要这些值onCreate方法完成之前。
How can I get width and height of a linear layout which is defined in xml as fill_parent both in height and width? I have tried onmeasure method but I dont know why it is not giving exact value. I need these values in an Activity before oncreate method finishes.
推荐答案
假如我得在XML中定义的的LinearLayout
宽度。我得通过XML它的参考。定义的LinearLayout
→
的实例。
Suppose I have to get a LinearLayout
width defined in XML. I have to get reference of it by XML. Define LinearLayout
l
as instance.
l = (LinearLayout)findviewbyid(R.id.l1);
ViewTreeObserver observer = l.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
init();
l.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});
protected void init() {
int a= l.getHeight();
int b = l.getWidth();
Toast.makeText(getActivity,""+a+" "+b,3000).show();
}
callfragment();
}
这篇关于获得布局的高度和宽度在运行时的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文