首页 > getLocationOnScreen()取值不准确问题

getLocationOnScreen()取值不准确问题

代码如下:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/milkwhite"
    tools:context="cn.com.dareway.employeecard.fragment.QrCodeFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="下拉刷新"
                android:textSize="20sp"
                android:textColor="@color/grey"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/qrcode"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_gravity="center" />
        </LinearLayout>

    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>


int[] location = new int[2];
qrcode.getLocationOnScreen(location);

获取到的x值是90,目测应该在300以上,这是怎么回事呢?


注意这个getLocationOnScreen()获取的是view在整个屏幕内的绝对坐标,且要从屏幕顶端算起,也就是包括了通知栏的高度。


没看出来你的 x 会有 90,倒是 y 有可能。你的 activity layout是怎样的?就是这个吗?

【热门文章】
【热门文章】