android - getWidth 和 getHeight 返回零

标签 android view

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

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

但是,绘图工作正常,我对宽度和高度进行了硬编码,所以它可以工作。为什么要这样做?


我的 View 类

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
    }

Activity 类

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>

最佳答案

直到 View 实际渲染到屏幕上才定义宽度和高度。

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

关于android - getWidth 和 getHeight 返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6145001/

相关文章:

java - 为什么 Android API 要求结果参数而不是返回方法?

android - 从 Android 上不同任务堆栈中的另一个应用程序启动应用程序

android - 从另一个 View 更改 TextView 文本

android - 更改自定义 CursorAdapter 的行布局

dynamic - View 中的 Couchbase 通配符/变量键

android - ImageView 未显示在 TableRow 上

Android AWS SDK 只返回一个 EC2 实例而不是列表

android - 使用 OpenCV 检测账单

MySql查询: Select top 3 rows from table for each category

view - 带有 Entity Framework 模型的 Oracle 物化 View