Android的ImageView的回报的getWidth

Android的ImageView的回报的getWidth

本文介绍了Android的ImageView的回报的getWidth,的getHeight零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有withh的ImageView的的getWidth /的getHeight功能的问题。我的XML如下:

I am having problem withh the getWidth / getheight function of ImageView.My xml is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bonus_popup"
    android:visibility="gone"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/todaybonus"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:src="@drawable/startscreen_todaybonus" />
    <ImageView android:id="@+id/ok_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="acceptBonus"
        android:background="#000000"></ImageView>
</RelativeLayout>

和我的code是如下:

and my code is as follows:

if (isDisplayBonus){
    set_blur_background();
    bonusPopup.setVisibility(0);
    Log.i(TAG, "Bonus width: " + todayBonus.getWidth());
    Log.i(TAG, "Bonus height: " + todayBonus.getHeight());
}

请帮我解决这个proble,。谢谢你在前进。

Please help me solve this proble,. Thank you in advance.

推荐答案

好了,我已经找到了原因。当我设置的知名度,认为实际上并没有被抽,所以我不能使用的getHeight和功能的getWidth。我找到了解决办法:使用ViewTreeObserver

Well, I have found the reason. When I set the visibility, the view had not actually been drawn, so I couldn't use getHeight and getWidth function.I found the solution: use ViewTreeObserver

todayBonus.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
        int finalHeight = todayBonus.getMeasuredHeight();
        int finalWidth = todayBonus.getMeasuredWidth();
            // Do your work here
            return true;
    }
    });

这篇关于Android的ImageView的回报的getWidth,的getHeight零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:39