android - getY 在片段中使用时返回 0.0
问题描述
我已经在这里尝试过答案: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>
返回 0.0。我需要这些图像视图的位置,因为我正在以编程方式添加更多视图。从代码中可以看出,我已经尝试了 URL 中的解决方案,但它不起作用。顺便说一句,我在标题中添加了“用于片段”,因为上面的代码写在片段中的事实可能与它不起作用的事实有关。
解决方案
View.getY()
不会给你视图的高度,而是Y
容器内视图左上角的位置。相反,您想使用View.getHeight()
,它将以像素为单位返回视图的高度(不是 DP!)。
推荐阅读
- system-verilog - 当应用于 SystemVerilog 接口时,“虚拟”是什么意思?
- uvm - `uvm_do_with 的问题:无法生成更多数据
- javascript - Vuex - 在状态中使用“this”关键字访问子组件中的方法
- sql - 计算两个不同表的两列之间的差异
- c++ - C ++:声明为extern时对const指针的未定义引用
- html - 我的 Angular 应用程序中没有显示任何内容
- node.js - 如何使用 apollo-server-cache 在外部获取缓存数据?
- php - 我可以覆盖 PHP 函数 echo() 吗?
- java - 从android中的文件路径获取内容uri for video
- python - 如何将纬度/经度坐标分配给python中的数据集?