【问题标题】:How to get the position of a dynamically added view relative to its parent如何获取动态添加的视图相对于其父视图的位置
【发布时间】:2015-06-15 18:05:37
【问题描述】:
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>

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

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

【问题讨论】:

    标签: android layout view position coordinate


    【解决方案1】:

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

    引用文档:

    getLeft()

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

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

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

    所以现在您正在获取相对于其父级的视图位置。也许您忘记在RelativeLayout.Params 上添加一些规则?

    【讨论】:

    • 我编辑了我的帖子,添加了一些屏幕截图。请看一看。我可以知道你在我的 RelativeLayout.Params 上添加一些规则是什么意思吗?
    猜你喜欢
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多