概念
getLocationInWindow是以B为原点的C的坐标
getLocationOnScreen是以A为原点的C的坐标
没有弹出窗口位置效果截图:
弹出窗口后位置效果截图:
总结:所以以上2种方法得到绝对位置的方法只在有弹出窗时会有区别。
可参考源码:
package com.example.zdemo_getlocation;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView _tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv= (TextView) findViewById(R.id.tv);
_tv.postDelayed(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
StringBuilder sb=new StringBuilder();
sb.append("父控件相对于它的父控件左上角位置:");
sb.append(((ViewGroup)_tv.getParent()).getLeft()+","+((ViewGroup)_tv.getParent()).getTop()+"\n");
sb.append("\n当前控件在相对父控件左上角位置:" + _tv.getLeft() + "," + _tv.getTop()+"\n");
int[] loc1=new int[2];
int[] loc2=new int[2];
_tv.getLocationInWindow(loc1);
_tv.getLocationOnScreen(loc2);
sb.append("\n当前控件相对于窗口左上角位置");
sb.append("\ngetLocationInWindow"+"("+loc1[0]+","+loc1[1]+")\n");
sb.append("\n当前控件相对于屏幕左上角位置");
sb.append("\ngetLocationOnScreen"+"("+loc2[0]+","+loc2[1]+")\n");
_tv.setText(sb.toString());
}
}, 2000);
findViewById(R.id.btn).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
final TextView tv=new TextView(MainActivity.this);
builder.setView(tv);
builder.create().show();
tv.postDelayed(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
StringBuilder SB=new StringBuilder();
int[] loc1=new int[2];
int[] loc2=new int[2];
tv.getLocationInWindow(loc1);
tv.getLocationOnScreen(loc2);
SB.append("当前控件相对于窗口左上角位置");
SB.append("\ngetLocationInWindow"+"("+loc1[0]+","+loc1[1]+")\n");
SB.append("\n当前控件相对于屏幕左上角位置");
SB.append("\ngetLocationOnScreen"+"("+loc2[0]+","+loc2[1]+")\n");
tv.setText(SB.toString());
}
}, 1000);
}
});
}
}
资源文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zdemo_getlocation.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="显示窗口控件位置"
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>