在片段中使用时,getY返回0.0

时间:2019-06-29 20:21:58

标签: android

我已经在这里尝试了答案:View getX() and getY() return 0.0 after they have been added to the Activity

private float foundHeight;
foundationZero = (ImageView) rootView.findViewById(R.id.foundation_zero);
        foundationZero.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Encuentra la posición de las found para comenzar a posicionar los demás elementos
                foundHeight = foundationZero.getY();
                foundationZero.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                System.out.println("HEIGHT ---->: " + foundHeight);}
        });

XML的基础是:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_fragment_view"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/gameInformationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="@dimen/informationMargins"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/scoreString"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/movString"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:layout_marginRight="@dimen/informationMargins"
            android:text="@string/timeString"/>
    </RelativeLayout>

    <!-- Static Layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <ImageView
            android:id="@+id/redeal_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/foundationBetweenCardMargin"
            android:layout_marginLeft="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:src = "@drawable/b1fv"/>
        <ImageView
            android:id="@+id/foundation_zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_one"
            android:layout_toLeftOf="@+id/foundation_one" />
        <ImageView
            android:id="@+id/foundation_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_two"
            android:layout_toLeftOf="@+id/foundation_two" />
        <ImageView
            android:id="@+id/foundation_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_three"
            android:layout_toLeftOf="@+id/foundation_three" />
        <ImageView
            android:id="@+id/foundation_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin" />


    </RelativeLayout>


</FrameLayout>

In返回0.0。我需要这些图像视图的位置,因为我正在以编程方式添加更多视图。从代码中可以看出,我已经在URL中尝试了该解决方案,但是它不起作用。顺便说一下,我在标题中添加了“用在片段中”,因为上面的代码是用Fragment编写的,这可能与它不起作用有关。

1 个答案:

答案 0 :(得分:0)

View.getY()并不能为您提供视图的高度,而是在容器内视图的左上角的Y位置。相反,您想使用View.getHeight(),它将以像素为单位返回视图的高度(不是DP!)。