Android得到控件在屏幕中的坐标

getLocationOnScreen ,计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度)//获取在当前屏幕内的绝对坐标

getLocationInWindow ,计算该视图在它所在的widnow的坐标x,y值,//获取在整个窗口内的绝对坐标 (不是很理解= =、)

getLeft , getTopgetBottomgetRight, 这一组是获取相对在它父亲里的坐标

如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。

[java] view plaincopy

package xiaosi.location;  

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;  

public class LocationActivity extends Activity {
    /** Called when the activity is first created. */
    private ImageView t = null;
    private Button button = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        t = (ImageView)findViewById(R.id.l);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new buttonListener());
    }
    public class buttonListener implements OnClickListener{  

        public void onClick(View v)
        {
            int[] location = new int[2];
            t.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            System.out.println("x:"+x+"y:"+y);
            System.out.println("图片各个角Left:"+t.getLeft()+"Right:"+t.getRight()+"Top:"+t.getTop()+"Bottom:"+t.getBottom());
        }
    }
}

  

view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >  

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="button"/>
    <ImageView
        android:id="@+id/l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/a" />
</LinearLayout>  

http://blog.csdn.net/sjf0115/article/details/7306284

Android得到控件在屏幕中的坐标

时间: 2024-08-06 11:53:18

Android得到控件在屏幕中的坐标的相关文章

Android 获得控件在屏幕中的坐标 - 总结

Android坐标原点为左上角,如果是某个View,那么就以该矩阵的左上角为原点 1.绝对坐标 Location int[] location = new int[2] ; view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标,含toolBar view.getLocationOnScreen(location); //获取在整个屏幕内的绝对坐标,含statusBar // location [0]--->x坐标,location [1]--->

android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen

int[] location = new  int[2] ;view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标location [0]--->x坐标,location [1]--->y坐标 getLocationOnScreen 得到该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度)//获取在当前屏幕内

Android RelativeLayout中实现控件平分屏幕

? 1 <!-- ? 1 RelativeLayout 达到平分效果 ? 1 --> ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <br><RelativeLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content">     <View<br&g

ArcGIS for Android地图控件的5大常见操作

原文地址: ArcGIS for Android地图控件的5大常见操作 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/arcgis_mobile/article/details/7801467   GIS的开发中,什么时候都少不了地图操作.ArcGIS for Android中,地图组件就是MapView,MapView是基于Android中ViewGroup的一个类(参考),也是ArcGIS Runtime SDK for

Android 使控件位于界面底部

Android 如何使控件位于界面底部 使控件位于界面底部有很多种办法,下面我就分情况说一下: 一.LinearLayout布局中: 把握三个原则即可轻松使控件位于界面底部: 1.最外层父容器LinearLayout设置高度  layout_height="match_parent" 2. 内层LinearLayout设置    layout_weight="1" 并且 layout_height="0dp" 3.内层LinearLayout 设

[Android]界面控件

1. 引用系统自带样式 字体大小 对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有时需要控制字体的大小.Android平台定义了三种字体大小. "?android:attr/textAppearanceLarge" "?android:attr/textAppearanceMedium" "?android:attr/textAppearanceSmal

Android基本控件之Menus

在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menus.就是菜单的意思,他基本分为三种:选项式菜单.上下文菜单.弹出式菜单 我们先来看第一种:选项式菜单 那么什么是选项式菜单呢? 就是当我们点击一个菜单的时候会弹出来一个菜单 如上图所示,右上角的那三个小点 就是菜单按钮,当我们点击时就会弹出一个菜单,这就是一个选项是菜单,既然我们知道是什么东西了,我

【Android的从零单排开发日记】之入门篇(十三)——Android的控件解析

Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.Android的控件结构 Android中有一类重要的控件,称为容器控件,它们派生自View的子类android.view.ViewGroup.每个容器控件都可以作为父控件存在,其中包含若干个子控件,每个子控件占据其父控件的一块区域,统一由父控件进行管理和支配. 交互界面中控件的关系直观上是极其复杂的,

Android必备:Android UI控件的了解与学习

看这里:Android必备:Android UI控件的了解与学习 由于工作需要,最近一段时间,需要进行Android App开发的学习,之前简单的进行过Android的了解,对于基本的Android环境的搭建等已经有过整理,一个Android App是由一个或多个Activity组成,每一个Activity都是一个UI容器,也就是一个屏幕界面,一个界面的组成则是由一组Android UI控件组成,本篇,我们就来简单的对Android UI控件进行初步的了解和学习. Android UI控件根据其