android 获取view在屏幕中的位置

使用view中的getLocationOnScreen方法,即可:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. final int[] locations = new int[2];  
  2. Button btn = (Button) findViewById(R.id.btn);<span style="font-family: Arial, Helvetica, sans-serif;">     </span>  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. btn.setOnClickListener(new OnClickListener() {  
  2.           
  3.     @Override  
  4.         public void onClick(View v) {  
  5.     // TODO Auto-generated method stub  
  6.          v.getLocationOnScreen(locations);  
  7.          Toast.makeText(MainActivity.this"x is: " + locations[0], Toast.LENGTH_SHORT).show();  
  8.          Toast.makeText(MainActivity.this"y is: " + locations[1], Toast.LENGTH_SHORT).show();  
  9.     }  
  10. });  
原文地址:https://www.cnblogs.com/xiaochao1234/p/4351002.html