Android 获取View的 left、right、top、buttom、以及x、y坐标的方法
2016-07-28 19:12
555 查看
xml文件如下:
一、View.getLeft()、getRight()、getTop()、getBottom
注意:getLeft()、getRight()、getTop()、getBottom 获取的值是相对于父布局的
所以上面的Button 的getLeft() = 0、getRight() = 150、getTop() = 400 、getBottom = 550。
下面的button的getleft() = 20,因为相对于他的父布局为20px (android:layout_x=”20px).
getRight = 20+200 ;
getTop = 0;()
getButtom = 0+150 ;
二、View.getLocationOnScreen(location)
计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度,除非在MainActivity中调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏)
其中 location = new int [2];
int x = location[0];
int y = location[1];
1.当调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏
上面的Button 的 x = 0; y = 400
下面的 Button 的 x = 180 +20;(表示相对于屏幕左边缘的位置)
y = 400;
当没调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏则 y的值会大于400,
因为此时 要加上标题栏的高度
三、方法如下:
ViewGroup vg = ((ViewGroup)getWindow().getDecorView());
Rect mRect = new Rect();
mButton2.getFocusedRect(mRect);
vg.offsetDescendantRectToMyCoords(mButton2, mRect);//获取view 的Rect(距离屏幕的)
Log.e(“ccc”, “left = “+mRect.left+” , right = “+mRect.right+”, top = “+mRect.top+”, bottom = “+mRect.bottom);
获取的也是相对于屏幕的值。
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/id_scale_btn" android:layout_width="150px" android:layout_height="100px" android:layout_y="400px" android:text="scalse" android:textSize="40px" <AbsoluteLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="180px" android:layout_y="400px" > <Button android:id="@+id/id_change_layout" android:layout_width="200px" android:layout_height="150px" android:text="change layout" android:layout_x="20px" android:textSize="40px" /> </AbsoluteLayout> </AbsoluteLayout>
一、View.getLeft()、getRight()、getTop()、getBottom
注意:getLeft()、getRight()、getTop()、getBottom 获取的值是相对于父布局的
所以上面的Button 的getLeft() = 0、getRight() = 150、getTop() = 400 、getBottom = 550。
下面的button的getleft() = 20,因为相对于他的父布局为20px (android:layout_x=”20px).
getRight = 20+200 ;
getTop = 0;()
getButtom = 0+150 ;
二、View.getLocationOnScreen(location)
计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度,除非在MainActivity中调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏)
其中 location = new int [2];
int x = location[0];
int y = location[1];
1.当调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏
上面的Button 的 x = 0; y = 400
下面的 Button 的 x = 180 +20;(表示相对于屏幕左边缘的位置)
y = 400;
当没调用requestWindowFeature(Window.FEATURE_NO_TITLE)来取消标题栏则 y的值会大于400,
因为此时 要加上标题栏的高度
三、方法如下:
ViewGroup vg = ((ViewGroup)getWindow().getDecorView());
Rect mRect = new Rect();
mButton2.getFocusedRect(mRect);
vg.offsetDescendantRectToMyCoords(mButton2, mRect);//获取view 的Rect(距离屏幕的)
Log.e(“ccc”, “left = “+mRect.left+” , right = “+mRect.right+”, top = “+mRect.top+”, bottom = “+mRect.bottom);
获取的也是相对于屏幕的值。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories