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

时间:2015-06-15 18:05:38

标签: android layout view position coordinate

RelativeLayout.LayoutParams imageParams= new RelativeLayout.LayoutParams(200,200);
ImageView mImageView=new ImageView(this);
mImageView.setImageBitmap(bitmap);
viewParent.addView(mImageView, imageParams);

在我的触摸事件中,我记录了我的视图中的getX,getLeft和leftMargin。

18835-18835/com.android.test E/﹕ getX: 260.49933 getLeft: 0.0 leftMargin: 0.0

为什么getLeft和leftMargin会返回0?如何获得视图相对于其父级的位置?

修改

XML

<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>

我以编程方式将imageview添加到相对布局中。然后我可以将图像拖放到视图内的任何位置

enter image description here

06-16 02:34:45.858    2721-2721/com.android.test E/﹕ getX: 206.57422getLeft: 0.0leftMargin: 0.0

enter image description here

06-16 02:43:28.566    2721-2721/com.now.stickit E/﹕ getX: 225.54785getLeft: 0.0leftMargin: 0.0

1 个答案:

答案 0 :(得分:0)

最好为您的问题提供可视化图像。

引用文档:

  

getLeft()

     

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

所以你要在RelativeLayout中添加你的视图,它会转到左上角。这是正常行为,getLeft()getLeftMargin()将返回0.

  

如何获得视图相对于其父级的位置?

所以现在你的相对于它的父母的观点位置。也许您忘记在RelativeLayout.Params上添加一些规则?