如何正确调整包含视图的大小

发布于 2024-08-30 11:59:14 字数 453 浏览 2 评论 0原文

我有一个 Activity ,它将显示由两部分组成的自定义视图。我希望一部分为可见屏幕高度的 1/3,另一部分为 2/3。

我可以重写 onMeasure() 并使用显示指标来查找显示器的高度,但这并不能说明电池栏或视图标题的大小。

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

如何判断可显示区域的高度?我准备覆盖 layout 或其他内容。 Android 的最佳实践是什么?我已经看到了其他类似的问题,但它们还没有结论。

I have an Activity that will display a custom view made up of 2 parts. I want one part to be 1/3 of visible screen height, the other part to be 2/3.

I can override onMeasure() and use display metrics to find the height of the display, but this does not account for the battery bar or view title sizes.

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

How can I tell the height of the displayable area? I'm prepared to override the layout or whatever. What is the Android's best practices? I've seen other questions along this line, but they are inconclusive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-09-06 11:59:14

我想出了一个办法。我重写RelativeLayout,并让它保留指向我的上视图和下视图的指针。然后,当它处理 onMeasure 时,它​​会在我的 2 个视图上设置所需的高度。他们在处理 onMeasure 时使用所需的高度。这给了我一个位于可见区域 2/3 的视图,另一个视图位于下方。

-- 来自我的 Layout 类

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;
}

I figured out a way to do it. I override RelativeLayout, and get it to keep pointers to my upper and lower views. Then when it processes onMeasure, it sets the desired heights on my 2 views. They use the desired height when they handle onMeasure. This gives me one view at 2/3 of the visible area, with the other one below.

-- from my Layout class

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);

}

-- from the View derived classes

@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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文