Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageView高度始终为0_Android_Android Imageview - Fatal编程技术网

Android ImageView高度始终为0

Android ImageView高度始终为0,android,android-imageview,Android,Android Imageview,我正在尝试获取imageview的高度和宽度,以便可以将图像缩小到适当的大小,但每次尝试获取高度时,高度都为0。我想可能它还没有被测量,所以我在imageview上放置了一个on predrawListener,这样我就知道它会在那个时候被测量,但我仍然得到0,所以我不确定问题出在哪里 这是我的全部布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.an

我正在尝试获取imageview的高度和宽度,以便可以将图像缩小到适当的大小,但每次尝试获取高度时,高度都为0。我想可能它还没有被测量,所以我在imageview上放置了一个
on predrawListener
,这样我就知道它会在那个时候被测量,但我仍然得到0,所以我不确定问题出在哪里

这是我的全部布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/content"
        android:layout_above="@+id/comment_layout">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"/>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/comments"
            android:background="@android:color/transparent"/>

    </RelativeLayout>

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

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:layout_alignParentBottom="true"
                android:layout_toLeftOf="@+id/send_button"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/send_button"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

</RelativeLayout>
宽度显示为720,但高度始终显示为0


输入硬宽度也没有效果

这应该适用于像这样的
OnGlobalLayoutListener

    final ImageView image = new ImageView(this);
    image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int height = image.getHeight();
        }
    });

当全局布局状态或可见性更改时调用此回调

检查这与使用
onPreDraw
有何不同?该视图应在该位置测量point@Vinayak.B它仍然给我0你可以检查高度后,分配给它的图像,然后它将是非零value@Chaitanya但这是我不能做到的,因为我需要尺寸,这样我就知道如何缩放全尺寸图像
    final ImageView image = new ImageView(this);
    image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int height = image.getHeight();
        }
    });