不能让一个按钮的android系统中的绝对坐标 [英] cant get the absolute coordinates of a button in android

查看:174
本文介绍了不能让一个按钮的android系统中的绝对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索按钮的绝对坐标。我也做了以下编码,但其总是返回0,任何人都可以帮助我。

 按钮B =(按钮)findViewById(R.id.button1);    INT [] =位置INT新[2];
    b.getLocationOnScreen(位置);    的for(int i = 0; I< location.length;我++)
    {
        Log.i(HHHHHHHH,+位置[I]);
    }
    Log.i(喜妇女参与发展,+宽+,+高);


解决方案

查看绘制后,你可以得到的位置。试试下面的的onCreate()code。

  ViewTreeObserver VTO = b.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){        @覆盖
        公共无效onGlobalLayout(){
            INT [] =位置INT新[2];
            b.getLocationOnScreen(位置);
        }
});

I am trying to retrieve the absolute coordinates of a button. I have done the following coding but its always returning 0. can anyone help me out.

       Button b=(Button)findViewById(R.id.button1);

    int[] location = new int[2];
    b.getLocationOnScreen(location);

    for(int i=0;i<location.length;i++)
    {
        Log.i("hhhhhhhh",""+location[i]);
    }
    Log.i("wid hei",""+width+","+height);

解决方案

You can get location after view is drawn. Try following code in onCreate().

ViewTreeObserver vto=b.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int[] location = new int[2];
            b.getLocationOnScreen(location);
        }
});

这篇关于不能让一个按钮的android系统中的绝对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆