如何创建具有相对尺寸的自定义视图? [英] How can I create a custom View with a Relative Size?

查看:108
本文介绍了如何创建具有相对尺寸的自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个可以与任何屏幕分辨率的设备一起使用的应用程序.

I would like to design an application which could work with devices of any screen resolution.

我这样做的想法是为我使用的每种视图类型创建一个自定义视图,并使用XML中指定的layout_widthlayout_height作为父级大小的百分比(例如,layout_width="100dp"等价于fill_parentlayout_width="50dp"表示View的宽度将是父级宽度的一半).

My idea to do so was to create one Custom View for each type of View I use, and use the layout_width and layout_height specified in the XML as a percentage of the parent size (for example, layout_width="100dp" is equivalent to fill_parent, and layout_width="50dp" means the View's width will be half the parent's one).

这是我制作的自定义TextView类:

Here is the custom TextView class I made:

public class RSTextView extends TextView {

    public RSTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int w, int h) {
        int parentWidth = ((View)this.getParent()).getWidth();
        int parentHeight = ((View)this.getParent()).getHeight();
        Log.i(null, "Width: " + w + "; Height: " + parentHeight);
        this.setMeasuredDimension(parentWidth * MeasureSpec.getSize(w) / 100,
                parentHeight * MeasureSpec.getSize(h) / 100);
    }
}

但是,它不起作用. parentWidthparentHeight的大小始终为0.

However, it does not work. The parentWidth and parentHeight size is always 0.

有没有更好的方法来创建具有相对大小的应用程序?

Is there any better way to create an application with a relative size?

如果这是正确的方法,如何从onMeasure方法中检索View父母的身高?

And if it is the right way to go, how can I retrieve the View's parents' size from the onMeasure method?

推荐答案

由于您正在布局阶段检查视图的高度,因此您可能希望使用 LinearLayout 权重.例如,如果您有一个带有三个子级的水平LinearLayout,则可以轻松地设置第一个,使其占据其宽度的一半,而其他两个则分别占其宽度的25%,方法是分别给它们分配0px layout_width s和layout_weight分别为2、1和1(或这些比例的任何其他权重).

Since you're checking a view's height during the layout phase, you probably want to be using getMeasuredHeight, not getHeight, but I'm not sure you need to be doing what you're doing at all. It's hard to tell from your description, but it seems like you might be trying to recreate the behavior of LinearLayout weights. For example, if you have a horizontal LinearLayout with 3 children, you can easily set the first to take up half its width and the others each to take up 25% by assigning them 0px layout_widths and layout_weights of 2, 1 and 1 respectively (or any other weights of those proportions).

但是,如果您确实决定采用自定义的View路线,请不要重载layout_heightlayout_width所做的工作-使用自定义XML属性来获取百分比的参数(我会深入探讨LinearLayout的代码以查看layout_weight的实现方式.)

If you do decide to go the custom View route, though, don't overload what layout_height and layout_width do -- use custom XML attributes to take the argument for the percentages (I'd dig into LinearLayout's code to see how layout_weight is implemented).

这篇关于如何创建具有相对尺寸的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆