Android得到控件在屏幕中的坐标
getLocationOnScreen ,计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度)//获取在当前屏幕内的绝对坐标
getLocationInWindow ,计算该视图在它所在的widnow的坐标x,y值,//获取在整个窗口内的绝对坐标 (不是很理解= =、)
getLeft , getTop, getBottom, getRight, 这一组是获取相对在它父亲里的坐标
如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。
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[];
t.getLocationOnScreen(location);
int x = location[];
int y = location[];
System.out.println("x:"+x+"y:"+y);
System.out.println("图片各个角Left:"+t.getLeft()+"Right:"+t.getRight()+"Top:"+t.getTop()+"Bottom:"+t.getBottom());
}
}
}
<?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>
Android得到控件在屏幕中的坐标的更多相关文章
- Android获得控件在屏幕中的绝对坐标
int[] location = new int[2] ; view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标 view.getLocationOn ...
- android 获取屏幕的高度和宽度、获取控件在屏幕中的位置、获取屏幕中控件的高度和宽度
(一)获取屏幕的高度和宽度 有两种方法: 方法1: WindowManager wm = (WindowManager) getContext().getSystemService(Context.W ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- Android基本控件之Menus
在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...
- 【Android开发日记】之入门篇(十三)——Android的控件解析
Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- android课程表控件、悬浮窗、Todo应用、MVP框架、Kotlin完整项目源码
Android精选源码 Android游戏2048 MVP Kotlin项目(RxJava+Rerotfit+OkHttp+Glide) Android基于自定义Span的富文本编辑器 android ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- Android 基本控件相关知识整理
Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...
随机推荐
- Linux调试工具strace和gdb常用命令小结
strace和gdb是Linux环境下的两个常用调试工具,这里是个人在使用过程中对这两个工具常用参数的总结,留作日后查看使用. strace调试工具 strace工具用于跟踪进程执行时的系统调用和所接 ...
- 关于bootstrap--表单(按钮<button>效果、大小、禁用)
1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...
- loadrunner java 缺少必要的导入包报错
loadrunner 运行从eclipse中做好的脚本,ctrl + A 复制到loadrunner中来, 添加参数化的的语句:verifyCode = lr.eval_string (&quo ...
- banner背景通栏
background: #76CEF6 url("../images/bg.jpg") repeat-x 0 0; -webkit-background-size: 100%; ...
- javascript 如何继承父类
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式修改
作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...
- android studio 报ambiguous method call
如题,在android studio中调用this.toString时,提示的错误信息是ambiguous method call. both get class () in object and g ...
- Asp.net原理(第一篇)
Asp.net (第一篇) 当用户在浏览器输入一个URL地址后,浏览器会发送一个请求到服务器.这时候在服务器上第一个负责处理请求的是IIS.然后IIS再根据请求的URL扩展名将请求分发给不同的ISAP ...
- puppet 4.4 System Requirements
puppet是linux下自动部署管理工具,有apply,agent/server两种模式,安装后默认为agent/server模式. apply模式下,每台机器均有自己的catalog文件,如果需要 ...
- 读取并解析properties文件
public class SysConfig { private static final Properties properties = new Properties(); static{ Reso ...