如何获取动态添加的视图相对于其父视图的位置 [英] How to get the position of a dynamically added view relative to its parent

查看:89
本文介绍了如何获取动态添加的视图相对于其父视图的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Under my touch event, i log getX, getLeft and leftMargin of my view.

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

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

Why getLeft and leftMargin return 0? How can i get the view's position relative to its parent?

编辑

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>

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

I added an imageview programmatically into the relative layout. Then I am able to drag and drop the image any where inside the view

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

推荐答案

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

It will be better to provide image for your question for visualization.

引用文档:

getLeft()

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

Left position of this view relative to its parent.

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

So you are adding your view inside RelativeLayout and it goes to top, left corner. It is normal behavior and getLeft() and getLeftMargin() will return 0.

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

How can i get the view's position relative to its parent?

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

So now your are getting your view position relative to it's parent. Maybe your forget to add some rules on your RelativeLayout.Params?

这篇关于如何获取动态添加的视图相对于其父视图的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆