【问题标题】:how to find position or coordinate of an Imageview inside a relativelayout? [duplicate]如何在相对布局中查找 Imageview 的位置或坐标? [复制]
【发布时间】:2018-01-28 16:17:28
【问题描述】:

这是我的页面布局。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/fullMeetingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_weight=".1"
        android:background="@color/colorPaleGrey2"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="20dp">

            <TextView
                android:id="@+id/meeting_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-medium"
                android:gravity="center_horizontal"
                android:text="Entrepreneur Meet Up"
                android:textColor="@color/blackDark"

                android:textSize="20.1sp"
                android:textStyle="normal" />

            <TextView
                android:id="@+id/time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/meeting_text"
                android:layout_centerInParent="true"
                android:layout_marginTop="10dip"
                android:fontFamily="sans-serif-medium"
                android:gravity="center_horizontal"
                android:lineSpacingExtra="6sp"
                android:text="(11:30AM - 4:00PM)"
                android:textColor="@color/bluey_grey_three"
                android:textSize="14sp"
                android:textStyle="normal" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rlmainLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.kdcos.contsync.utilities.dragview.DragLayer
                android:id="@+id/framelayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


            </com.kdcos.contsync.utilities.dragview.DragLayer>
            <RelativeLayout
                android:id="@+id/rl_dropView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/round_bg"
                android:padding="30dp">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/ic_drop_cards" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/imageView"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center_horizontal|center"
                    android:fontFamily="sans-serif"
                    android:gravity="center_horizontal"
                    android:lineSpacingExtra="10sp"
                    android:text="Drop Cards Here"
                    android:textColor="@color/blackDark"
                    android:textSize="10sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/delete_zone_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="bottom"
                android:orientation="horizontal"
                android:visibility="visible">

                <ImageView
                    android:id="@+id/deleteButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"                        android:padding="@dimen/margin_20dp"
                    android:background="@drawable/ic_delete" />
            </RelativeLayout>


        </RelativeLayout>



    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:layout_weight=".2">

        <android.support.v4.widget.DrawerLayout 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/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start">

            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                tools:context="com.guna.navigationviewonbothssides.MainActivity">

                <android.support.design.widget.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    app:elevation="0dp">


                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">


                        <android.support.design.widget.TabLayout
                            android:id="@+id/tabLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_toLeftOf="@+id/menuRight"
                            android:background="@color/colorWhite"
                            android:minHeight="?attr/actionBarSize"
                            app:tabGravity="fill"
                            app:tabIndicatorColor="@color/colorTurquoiseBlue"
                            app:tabIndicatorHeight="3dp" />

                        <RelativeLayout
                            android:id="@+id/menuRight"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:background="@color/blackDark"
                            android:padding="10dp"
                            android:tint="@color/colorPrimary">

                            <ImageButton
                                android:layout_width="16dp"
                                android:layout_height="16dp"
                                app:srcCompat="@drawable/ic_menu_white_24dp"

                                />

                        </RelativeLayout>


                    </RelativeLayout>

                </android.support.design.widget.AppBarLayout>

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager_meeting_landscape"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="50dp"
                    android:background="@color/colorWhite" />

            </android.support.design.widget.CoordinatorLayout>


            <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_gravity="end"

                android:fitsSystemWindows="true">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview_drawer_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="10dp"
                    android:visibility="visible"
                    android:scrollbars="vertical" />


                <TextView
                    android:id="@+id/empty_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingTop="10dp"
                    android:scrollbars="vertical"
                    android:gravity="center"
                    android:text="@string/no_meeting_fount"
                    android:visibility="gone" />
            </android.support.design.widget.NavigationView>
        </android.support.v4.widget.DrawerLayout>
    </LinearLayout>
</LinearLayout>

我想获取弹出位置坐标,以便在我删除列表视图项时可以执行一些操作

我尝试使用 getLocationInWindow() x 和 y 坐标,但我总是得到 x 轴 0 而不是 0 位置我必须获得弹出位置或删除按钮的区域,以便我可以触摸列表视图项目视图来删除位置建议我如何找到删除图像视图坐标或位置。

【问题讨论】:

  • getX() 和 getY() 属性有效吗?...尝试使用它们,我认为这就是你想要的
  • 另一件事是你想要相对于屏幕还是相对于父级的坐标?
  • 是的@AhmedRajab,但我总是得到错误的坐标,我把它放在底部删除图标,但它的显示 x 轴总是零
  • @AhmedRajab 你对这个解决方案有什么建议

标签: android


【解决方案1】:

我认为原因是当您尝试获取其坐标时,视图尚未在屏幕上绘制,这就是为什么它的 x 或 y 坐标总是返回 0

查看以下链接了解类似问题并从中获取解决方案 请参阅以下链接 Getting 0 Value for both X and Y Coordinate of view(Imagebutton) in android

【讨论】:

  • 我应用了相同的代码,但我不知道为什么我仍然得到零 x aixs。但 y 轴我得到了价值
  • 可能是图像有些奇怪......坐标是正确的,而 x 是错误的@ManojKumar
  • 实际上我采用了 Relatvie 布局,所以我得到了 0,现在我已经拍摄了图像按钮,所以它不起作用,但我需要帮助如何在视图上应用条件被拖动到那个点区域,你能建议我吗如何应用条件,例如我有 imageview 的坐标(510,960),当其他视图在拖动@Ahmed Rajab 后进入该区域时,我想得到回调
  • 我认为以下链接可能会有所帮助.....如果有帮助请接受我的回答
  • 链接@Ahmed Rajab
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多