Android~~获取view在屏幕中的位置

···

getLocalVisibleRect , 返回一个填充的Rect对象, 感觉是这个View的Rect大小,left,top取到的都是0

···

getGlobalVisibleRect , 获取全局坐标系的一个视图区域, 返回一个填充的Rect对象;该Rect是基于总整个屏幕的

···

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

···

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

···

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

···

**注**:如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些(个人感觉好像是得有焦点,不是很清楚,待查查)

example:

    int[] location = new int[2];
    v.getLocationOnScreen(location);
    int x = location[0];
    int y = location[1];

XML:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5px"
    >
            android:id="@+id/fliper"  
        android:layout_width="fill_parent"  
        android:layout_height="100dp"  
        android:background="#FFFFFF"
        android:flipInterval="2000" > 
                    android:id="@+id/tv_1" 
            android:text="Hello1"
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_horizontal"> 
        
                    android:id="@+id/tv_2" 
            android:text="Hello"
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"> 
         
    
    

转载于:https://www.cnblogs.com/520Dennan/archive/2011/07/19/2111143.html

你可能感兴趣的:(移动开发,ui)