1

アプリケーションが実行されます。Heightofthescreenそれがすべきことは、画面の実際の値であるテキストボックスに「=Heightofthescreen=」を表示することです。代わりに、しかし、それは私に 0 を与えるだけです。私はそれが私の文脈にあると思いますが、それを修正する方法がわかりません。

活動クラス:

package com.example.measuringtesting;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class StartDraw extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Drawing.context2 = getApplicationContext();
        Drawing y = new Drawing(Drawing.context2);
        int theheight = y.width2;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_draw);
        TextView t = new TextView(this); 

        t=(TextView)findViewById(R.id.textView1); 
        t.setText("=" + theheight + "=");
    }
}

ビュークラスは次のとおりです。

package com.example.measuringtesting;

import android.content.Context;
import android.view.View;

public class Drawing extends View {
public Drawing(Context context) {
    super(context);
}

public float width;
public float height;
public int width2;
public int height2;
public static Context context2;

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
float parentWidth = MeasureSpec.getSize(widthMeasureSpec);
float parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.width = (float) (parentWidth*.15);
this.height = (float) (parentHeight*.15);
this.width2 = Math.round(width);
this.height2 = Math.round(height);
this.setMeasuredDimension(width2, height2);
}

}
4

1 に答える 1

0

問題はonMeasure()、レイアウトが描画されるまで呼び出されないことです。これは、TextView でテキストを設定したときよりもしばらく後のことです。なので今のところy.width2まだ0です。

この質問を見てください:ビューを最初に測定できるのはいつですか?

基本的に、測定したい View にaGlobalLayoutListenerを追加する必要があります。ViewTreeObserverビューの測定値は、 のpublic void onGlobalLayout()コールバックで利用できますGlobalLayoutListener

于 2013-11-05T17:08:18.563 に答える