深刻理解getLocationInWindow 和 getLocationOnScreen区别

       

概念

 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>


 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值