getWidth和getHeight返回零

时间:2011-05-26 21:00:35

标签: android view

我正在尝试为Android制作一个简单的绘图程序。

我有一个自定义View类来处理绘图。当我调用getWidthgetHeight时,我得到零。

但是,绘图工作正常,我在宽度和高度上硬编码,所以它的工作原理。为什么要这样做?


我的观看课程

public class cDrawing extends View{

    char BitMap[];
    static final short WIDTH=160;
    static final short HEIGHT=440;
    static final char EMPTY=' ';
    int mWidthSize;
    int mHeightSize;

    static final char RED ='R';
    int y;

    public cDrawing(Context context) {
        super(context);

        y=3;
        // set up our bitmap
        BitMap=new char[WIDTH*HEIGHT];
        for(int i=0; i<WIDTH*HEIGHT; i++)
                BitMap[i]=EMPTY;


        // returns zero why???????
        int h=getHeight();
        h=400;
        int w=getWidth();
        w=320;
        mWidthSize=w/WIDTH;
        mHeightSize=h/HEIGHT;


        // TODO Auto-generated constructor stub
    }

活动类

public class cCanves extends Activity  implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canves);
        cDrawing board=new cDrawing(this);


        LinearLayout layout = (LinearLayout)findViewById(R.id.parent2);
        layout.addView(board);


        // set up buttons
        View mEraser = findViewById(R.id.buteraser);
        mEraser.setOnClickListener(this);

        View mBlack = findViewById(R.id.butblack);
        mBlack.setOnClickListener(this);

        View mWhite = findViewById(R.id.butwhite);
        mWhite.setOnClickListener(this);

        View mRed = findViewById(R.id.butred);
        mRed.setOnClickListener(this);

    } // end function

    public void onClick(View v) {
        Intent i;
        switch(v.getId())
        {
        case R.id.buteraser:
            break;
        case R.id.butblack:
            break;
        case R.id.butwhite:
            break;
        case R.id.butred:
            break;
        } // end switch

    }   // function
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent">

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:orientation="horizontal"
      android:layout_height="wrap_content">

    <ImageButton android:id="@+id/buteraser"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butblack"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butwhite"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/butred"
      android:src="@drawable/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout android:id="@+id/parent2"
      android:layout_width="fill_parent"
      android:orientation="vertical"
      android:layout_height="fill_parent">
    </LinearLayout>


</LinearLayout>

6 个答案:

答案 0 :(得分:19)

在视图实际渲染到屏幕之前,不会定义宽度和高度。

使用protected abstract void onLayout (boolean changed, int l, int t, int r, int b)(您在活动中覆盖)来了解尺寸何时准备就绪。

答案 1 :(得分:6)

补充Mah的答案,我发现你可以从参数中获取值,如下面的代码:

ImageView imageProcess = (ImageView) li.inflate(
    R.layout.andamento_sinistro_imageprocess, centerLayout, false);
imageProcess.setBackgroundResource(
    (isActive)?(R.drawable.shape_processon):(R.drawable.shape_processoff));
RelativeLayout.LayoutParams imageProcessParams = 
    (RelativeLayout.LayoutParams)imageProcess.getLayoutParams();
imageProcessParams.leftMargin = 
    (int) (centerPosition - 0.5*imageProcessParams.width);
imageProcessParams.addRule(RelativeLayout.CENTER_VERTICAL);
centerLayout.addView(imageProcess);

这里真正的问题是LayoutParams的使用,它具有尚未由元素处理的规则。

答案 2 :(得分:3)

我不确定,但它可能与代码在您活动的lifecycle中的位置有关。如果您在屏幕上显示视图之前调用getWidth()getHeight(),那么您的值将为0.我也遇到过这种情况。

我不确定是否有解决方法。我不得不依靠获取硬件屏幕的宽度和高度,而不是视图的宽度和高度。您最终可能需要近似视图的宽度和高度并对其进行硬编码。

答案 3 :(得分:1)

您应该在重写方法 onLayout 中调用 getWidth() getHeight()

答案 4 :(得分:0)

  

只需使用getViewTreeObserver()侦听器,然后在其中   计算高度和宽度。

     

遵循代码:

getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //Do your Stuff calculation , like view.getWidth() ... });

答案 5 :(得分:-6)

视图第一次显示后只有尺寸。

其他事情:

int h=getHeight();
h=400;

没用没用?它只是用于测试吗?

相关问题