Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 相对于父视图的视图位置_Android_Android Layout_Coordinates - Fatal编程技术网

Android 相对于父视图的视图位置

Android 相对于父视图的视图位置,android,android-layout,coordinates,Android,Android Layout,Coordinates,我使用Android 5.1创建了一个自定义视图,如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/task_layo

我使用Android 5.1创建了一个自定义视图,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/task_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context="com.task.TaskActivity">

    <RelativeLayout
        android:id="@+id/task_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#e4e4e4"
        android:padding="10dp">

        <View
            android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="#000000" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/task_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@id/task_header"
        android:gravity="center">

        <View
            android:id="@+id/myRectangleView"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="316dp"
            android:layout_marginStart="316dp"
            android:layout_marginTop="114dp"
            android:background="@drawable/rectangle"
            android:visibility="invisible"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="RtlHardcoded" />

    </RelativeLayout>

</RelativeLayout>
    View rectangleView = findViewById(R.id.myRectangleView);
    rectangleView.getLocationOnScreen(location);
    int x = location[0];
    int y = location[1];
问题是,这给了我屏幕上的绝对位置,但我想要相对于
task\u标题的位置

如何执行此操作?

您可以尝试从每个视图中“向上”遍历,并在此过程中对每个容器位置求和:

/** Returns array of views [x, y] position relative to parent **/
public static int[] getPositionInParent(ViewGroup parent, View view){
    int relativePosition[] = {view.getLeft(), view.getTop()};
    ViewGroup currentParent = (ViewGroup) view.getParent();
    while (currentParent != parent){
        relativePosition[0] += currentParent.getLeft();
        relativePosition[1] += currentParent.getTop();
        currentParent = (ViewGroup) currentParent.getParent();
    }
    return relativePosition;
}

这没有错误处理,但即使在更嵌套的布局中也应该可以工作。

我在Kotlin中实现了一个简单的布局。如果您知道子参数视图的直接父级,这将起作用

        fun getPositionInParent(parent: ViewGroup, child: View): IntArray {
            val relativePosition = intArrayOf(child.left, child.top)
            val positionPreview = intArrayOf(1, 2)
            val positionFrame = intArrayOf(1, 2)
            parent.getLocationInWindow(positionPreview)
            child.getLocationInWindow(positionFrame)

            relativePosition[0] = positionFrame[0] - positionPreview[0]
            relativePosition[1] = positionFrame[1] - positionPreview[1]
            return relativePosition
        }

您是否尝试过只使用getX()、getY()或getLocalVisibleRect(Rect r)?