Android ConstraintLayout getWidth返回0

4

我在我的项目中使用了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());
}

布局设计视图

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。


1
可能是getWidth()和getHeight()返回0的视图的重复问题。 - Mike M.
问题是什么?问题及其答案可能对其他人很重要。 - nandsito
这是一个愚蠢的问题。所以我重新写了它。 - ittianyu
2个回答

7

OnCreate()方法中,根据你的xml参数,你的view尚未被创建。因此,你需要在你的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());
   }
});

1
该方法很有用。但是在API级别16中,该方法已被弃用。请改用#removeOnGlobalLayoutListener。 - ittianyu
如果将宽度设置为与父元素匹配,这会起作用吗? - the_prole

6
因为您的视图尚未被创建。
  iv.post(new Runnable() {
            @Override
            public void run() {
                 //height is ready
                 System.out.println("imageview width:" + iv.getWidth() + " height:" + iv.getHeight());
            }
        });

不需要谢谢,这是我的工作。 - Sohail Zahid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接