Android 如何获取动态添加的视图相对于其父视图的位置

Android 如何获取动态添加的视图相对于其父视图的位置,android,layout,view,position,coordinate,Android,Layout,View,Position,Coordinate,在我的触摸事件下,我记录视图的getX、getLeft和leftMargin RelativeLayout.LayoutParams imageParams= new RelativeLayout.LayoutParams(200,200); ImageView mImageView=new ImageView(this); mImageView.setImageBitmap(bitmap); viewParent.addView(mImageView, imageParams); 为什么ge

在我的触摸事件下,我记录视图的getX、getLeft和leftMargin

RelativeLayout.LayoutParams imageParams= new RelativeLayout.LayoutParams(200,200);
ImageView mImageView=new ImageView(this);
mImageView.setImageBitmap(bitmap);
viewParent.addView(mImageView, imageParams);
为什么getLeft和leftMargin返回0?如何获取视图相对于其父视图的位置

编辑

XML


最好为您的问题提供图像,以便可视化

引用文件:

getLeft()

此视图相对于其父视图的左侧位置

因此,您正在RelativeLayout中添加视图,它将进入左上角。这是正常行为,
getLeft()
getLeftMargin()
将返回0

如何获取视图相对于其父视图的位置


现在,您可以获得相对于其父视图的视图位置。也许你忘记在你的
RelativeLayout.Params上添加一些规则了?

我编辑了我的文章,添加了一些屏幕截图。请看一看。我可以知道你在我的相亲上添加一些规则是什么意思吗?
18835-18835/com.android.test E/﹕ getX: 260.49933 getLeft: 0.0 leftMargin: 0.0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.now.stickit.EditNoteActivity"
    android:id="@+id/edit_note_layout">

<RelativeLayout
    android:id="@+id/view_parent"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/background_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

    <ImageView
        android:id="@+id/canvas_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

</RelativeLayout>

   <LinearLayout

    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:gravity="center_horizontal"
    android:background="@color/primary">

   <ImageButton
       android:id="@+id/addText"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_add_text"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/draw"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_draw"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/erase"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_erase"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/addImage"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_add_image"
       android:background="#00000000"/>

   <ImageButton
       android:id="@+id/undo"
       android:layout_width="45dp"
       android:layout_height="45dp"
       android:src="@drawable/ic_action_undo"
       android:background="#00000000"/>
</LinearLayout>
</LinearLayout>
06-16 02:34:45.858    2721-2721/com.android.test E/﹕ getX: 206.57422getLeft: 0.0leftMargin: 0.0
06-16 02:43:28.566    2721-2721/com.now.stickit E/﹕ getX: 225.54785getLeft: 0.0leftMargin: 0.0