getWidth()返回0,如果由机器人设定:layout_width =" match_parent"机器人、getWidth、layout_width、match_parent

由网友(悲催的爱情=)分享简介:我有一类称为FractalView延伸ImageView的。我的目标是绘制一个分形与此类具有整个屏幕的大小。这是我的活动的布局文件的唯一视图,比顶级的LinearLayout其他:I have a class called FractalView that extends ImageView. My goal is...

我有一类称为FractalView延伸ImageView的。我的目标是绘制一个分形与此类具有整个屏幕的大小。这是我的活动的布局文件的唯一视图,比顶级的LinearLayout其他:

I have a class called FractalView that extends ImageView. My goal is to draw a fractal with this class that has the size of the whole screen. It is the only View in my activity's layout file, other than the top-level LinearLayout:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
    android:background="#FFFFFFFF">

    <com.antonc.fractal_wallpaper.FractalView
        android:id="@+id/fractal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>

正如你所看到的,我使用的是match_parent为layout_width和两个FractalView和的LinearLayout的layout_height。此时我假定FractalView的尺寸被设定为一定值时,因为它匹配它的父,它匹配它的父(它是整个屏幕)。 然后,我尝试检测,可用于FractalView使用的getWidth()和getHeight()尺寸:

As you can see, I'm using "match_parent" for both layout_width and layout_height of both the FractalView and the LinearLayout. At this point I assumed that the size of the FractalView is set to a certain value, since it's matching it's parent, which is matching it's parent(which is the whole screen). Then I try to detect the size that is available to FractalView by using getWidth() and getHeight():

public class FractalView extends ImageView {

private Context mContext;
private Handler mHandler;

private int mScrWidth;
private int mScrHeight;

public FractalView(Context context) {
    super(context);
    mContext = context;
    init();
}

private void init() {

    mScrWidth = getWidth();
    mScrHeight = getHeight();
    // Breakpoint
}

@Override
protected void onDraw(Canvas canvas) {
    [...]
}

}

但是,当我的code到达断点时,调试器显示mScrWidth和mScrHeight为零。我已经使用getMeasuredWidth()和getMaxWidth()试过了,但他们也返回不正确的值。我读过一些答案类似的问题,即这个,他们都解释说,getWidth()返回的大小图像的在的它已经绘就,而我真正需要的是区域的宽度这是提供给这种看法,的在的我开始绘制。

But when my code reaches the breakpoint, the debugger shows that mScrWidth and mScrHeight are zero. I've tried using getMeasuredWidth() and getMaxWidth(), but they also return incorrect values. I've read several answers to similar questions, i.e. this one and they all explain that getWidth() returns the size of an image after it has been drawn, while what I actually need is the width of the area which is available to this view, before I start drawing.

有没有一种方法来检索这些值的宽度和高度?

Is there a way to retrieve such values for width and height?

推荐答案

一个视图的尺寸不可后才 onMeasure(),特别是如果其设置为 WRAP_CONTENT FILL_PARENT

A view's size isn't available until after onMeasure(), particularly if its set to wrap_content, or fill_parent.

您应该​​访问的大小或之后 onMeasure()在视图code,或AA布局树观察报:

You should either access the size in or after onMeasure() in the View code, or in a a Layout Tree Observer:

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});

您还需要添加机器人:ID =@ + ID / mylayout您的LinearLayout的第二个工作

You will also need to add android:id="@+id/mylayout" to your LinearLayout for the second one to work.

阅读全文

相关推荐

最新文章