import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;/** * 如果在Activity的OnCreate()事件输出控件位置数据,是全为0,要等UI控件都加载完了才能获取到数据。 * @author ZLQ * */public class GetLocationData extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }        /*     * 单击按钮事件     */    public void click(View v){    View view = findViewById(R.id.button2);    //数组长度必须为2    int[] locations = new int[2];    view.getLocationOnScreen(locations);    int x = locations[0];//获取组件当前位置的横坐标    int y = locations[1];//获取组件当前位置的纵坐标    Log.i("System.out", "x:" + x + "y:" + y);    }}


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearlayout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <Button        android:id="@+id/button2"        android:layout_width="80dp"        android:layout_height="80dp"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="150dp"        android:onClick="click"        android:text="Button" /></RelativeLayout>


更多相关文章

  1. Android最新优秀而实用的开源组件1
  2. Android列表控件Spinner简单用法示例
  3. 高级控件1
  4. Android ImageView控件的MaxWidth、MaxHeight不起作用
  5. Android Jetpack架构组件Navigation管理Fragment框架
  6. android 圆形头像—— android开源系列:CircleImageView自定义圆
  7. android 概述 及四大组件
  8. Android 日期和时间选择控件的开发
  9. Android中自定义控件之飞入飞出布局及随机布局实现方式

随机推荐

  1. 自定义View系列教程06--详解View的Touch
  2. Android(安卓)Audio 数据流详解
  3. Android小技术知识(多用于面试)
  4. Android VR Player(全景视频播放器) [8]:Med
  5. Android(安卓)自定义密码输入框和键盘(仿
  6. React Native 集成Android友盟推送过程
  7. android使用websocket保持长连接 后台为n
  8. Android Handler内存泄漏解决方法
  9. [Android]应用语言切换的三种方法
  10. Android技术社区