Android ConstraintLayout getWidth 返回 0

标签 android

<分区>

我在我的项目中使用 ConstraintLayout。当我尝试在 onCreate 中调用 getWidth 时发现问题。结果为 0。

代码在这里。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paint);
    iv = (ImageView) findViewById(R.id.iv);
    iv.setOnTouchListener(new ImageViewTouchListener());

    System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
}

The layout design view

xml 内容。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_red"
        android:background="#ff0000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_green"
        android:background="#00ff00"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/btn_red" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_blue"
        app:layout_constraintLeft_toRightOf="@+id/btn_green"
        android:background="#0000ff"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="55dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.09784412" />

    <SeekBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/sb_stroke"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@+id/btn_green" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline4"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="103dp"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintGuide_percent="0.1840796" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/iv"
        app:layout_constraintTop_toTopOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:ignore="ContentDescription"
        android:background="#22000000"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

我想知道如何获得 ImageView 的正确宽度和高度。

我尝试在 onStart 和 onResume 中获取宽度和高度,但得到相同的结果 0。

最佳答案

OnCreate() 中,您的 view 尚未根据您的 xml 参数创建。因此,您必须在 View 中添加 addOnGlobalLayoutListener(),一旦创建了 view,您就会在该 listener 中获取尺寸。

iv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
   }
});

关于Android ConstraintLayout getWidth 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660918/

相关文章:

java - 在 Android 5.0+ 上在没有 viewPagerAdapter 的布局上显示 pagerSlidingTabStrip

Android:如何使用Retrofit获取 session ID?

java - 使用 charAt() 查找空格、换行符和制表符

java - 创建自定义微调器时如何实现 getView

java - 如何向 Eclipse 项目添加外部依赖项?

android - 如何使用 ffmpeg 从应用程序捕获的视频创建视频拼贴?

java - SSL 套接字 - Java 和证书?

android - 如何将对象从 fragment 传递/共享到 fragment ?

android - 数据库中的数据太多

java - 无法解析为 jodatime