安卓java代码中获取控件的高和宽(1)

来源:互联网 发布:sql server msde win7 编辑:程序博客网 时间:2024/05/24 22:46

1、背景:
在activity中可以调用View.getWidth、View.getHeight()、View.getMeasuredWidth() 、View.getgetMeasuredHeight()来获得某个view的宽度或高度,

(1)但是在onCreate()、onStrart()、onResume()方法中会返回0,
原因:
这是应为当前activity所代表的界面还没显示出来没有添加到WindowPhone的DecorView上或要获取的view没有被添加到DecorView上或者该View的visibility属性为gone 或者该view的width或height真的为0 所以只有上述条件都不成立时才能得到非0的width和height

2、解决:你要确定你的控件加载完成:
(1)、控件的回调方法

View view=findViewById(R.id.tv);        view.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {                int width = v.getWidth();            }        });  

(2)、view被显示出来后,就可以获得:

@Override   public void onWindowFocusChanged(boolean hasFocus) {        View iv1 = findViewById(R.id.iv1);    View iv2=findViewById(R.id.iv2);    String msg1="iv1' width:"+iv1.getWidth()+"  height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();    String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();    i("onWindowFocusChanged() "+msg1);    i("onWindowFocusChanged() "+msg2);        super.onWindowFocusChanged(hasFocus);      }  

(3)、onResume方法最后开线程300毫秒左右后获取宽和高
(view是在setContentView时设进去的View或它的子View)

view.postDelayed(new Runnable() {                @Override                public void run() {                    View iv1 = findViewById(R.id.iv1);                    View iv2=findViewById(R.id.iv2);                    String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();                    String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();                    i("onWindowFocusChanged() "+msg1);                    i("onWindowFocusChanged() "+msg2);                }            }, 300);  

(4)、在onCreate()或onResume()等方法中需要获取宽高时

使用getViewTreeObserver().addOnGlobalLayoutListener()来添为view加回调
在回调里获得宽度或者高度获取完后让view删除该回调

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                view.postDelayed(new Runnable() {                    @Override                    public void run() {                        View iv1 = findViewById(R.id.iv1);                        View iv2=findViewById(R.id.iv2);                        String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+"  measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();                        String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+"  measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();                        i("onWindowFocusChanged() "+msg1);                        i("onWindowFocusChanged() "+msg2);                    }                }, 300);            }        });  
0 0