0

Activity2 つの部分で構成されるカスタム ビューを表示する があります。1 つの部分を表示画面の高さの 1/3 にし、もう 1 つの部分を 2/3 にしたいと考えています。

ディスプレイ メトリックをオーバーライドonMeasure()して使用してディスプレイの高さを調べることはできますが、これはバッテリー バーやビュー タイトルのサイズを考慮していません。

DisplayMetrics dm = new DisplayMetrics(); 
((WindowManager)contxt.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm); 
int height = dm.heightPixels;

表示可能領域の高さを知るにはどうすればよいですか? layoutまたは何でもオーバーライドする準備ができています。Androidのベスト プラクティスは何ですか? この線に沿って他の質問を見てきましたが、決定的ではありません。

4

1 に答える 1

1

私はそれを行う方法を考え出しました。私はRelativeLayoutをオーバーライドし、それを取得して、上部ビューと下部ビューへのポインターを保持します。次に、onMeasureを処理するときに、2つのビューに目的の高さを設定します。onMeasureを処理するときに、必要な高さを使用します。これにより、表示領域の2/3に1つのビューが表示され、もう1つのビューが下に表示されます。

-私のレイアウトクラスから

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // set default screen heights
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if ( upper != null )
        upper.setPrefHeight((heightSize*2)/3);
    if ( lower != null )
        lower.setPrefHeight(heightSize/3);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

--View派生クラスから

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(measureWidth(widthMeasureSpec),
            measureHeight(heightMeasureSpec));
}

/**
 * Determines the width of this view
 * @param measureSpec A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        // We were told how big to be
        result = specSize;
    } else {
        DisplayMetrics dm = new DisplayMetrics(); 
        ((WindowManager)contxt.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm); 
        result = dm.widthPixels; 
    }
    width = result;
    return result;
}

/**
 * Determines the height of this view
 * @param measureSpec A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        // We were told how big to be
        result = specSize;
    } else {
        result = prefHeight;
    }
    height = result;

    return result;
}
于 2010-04-28T16:28:13.563 に答える