通知Android布局吹气多大" WRAP_CONTENT"应该 [英] Telling the Android layout inflater how large "wrap_content" should be

查看:128
本文介绍了通知Android布局吹气多大" WRAP_CONTENT"应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的查看,这样当它膨胀与 WRAP_CONTENT 作为维度之一参数和 match_parent 作为其他,就会产生一个恒定的高宽比,填充任何尺寸设置为 match_parent ,但提供吹气布局与其他维度是包装。我presume这是可能的,因为,例如,一个完整的屏幕宽度 TextView的显然能够要求它具有的空间为两个,三个或任何任意数目的文本行(取决于宽度),但不一定知道这一点,直到通胀的时间。

I want to create a custom View, such that when it is inflated with wrap_content as one of the dimension parameters and match_parent as the other, it will have a constant aspect ratio, filling whichever dimension is set to match_parent, but providing the layout inflater with the other dimension to be "wrapped". I presume this is possible because, for example, a full screen width TextView would obviously be able to demand that it have space for two, three or any arbitrary number of lines of text (depending on width), but would not necessarily know this until inflation-time.

理想情况下是我想要做的是在查看子类中覆盖的布局方法,例如,当视图充气时,我得到的布局信息,并提供自己的尺寸内容被包裹(即我的固定比例的矩形)。

Ideally what I want to do is override layout methods in the View subclass such that when the view is inflated, I get the layout information, and supply my own dimensions for the "content" to be wrapped (ie my fixed-ratio rectangle).

我将需要创建大量的这些自定义视图,并把它们在各种不同类型的布局有时会使用适配器 - 所以我真的希望有最大在他们的通货膨胀,我可以控制。什么是最好的方法这样做的?

I will need to create a lot of these custom views and put them in various different types of layout—sometimes using an Adapter—so really I want to have the maximum control over their inflation I can. What's the best technique for doing this?

推荐答案

我现在用下面的code解决了这个。值得一提轻描淡写地说,我重写类是一个自定义的的ViewGroup 定制儿童,全部采用继承 onMeasure 。孩子们创建,并在施工时添加的,我将承担为理所当然的事,这是必要的。

I've now solved this with the following code. It's worth mentioning in passing that the class I'm overriding is a custom ViewGroup with custom children, all using the inherited onMeasure. The children are created and added at construction-time, and I would assume as a matter of course that this is necessary.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    float width = MeasureSpec.getSize(widthMeasureSpec);
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    float height = MeasureSpec.getSize(heightMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    float nominalHeight = getResources().getInteger(R.integer.nominalheight);
    float nominalWidth = getResources().getInteger(R.integer.nominalwidth);

    float aspectRatio = nominalWidth / nominalHeight;

    if( widthMode == MeasureSpec.UNSPECIFIED ) { //conform width to height
        width = height * aspectRatio;
    }
    else if (heightMode == MeasureSpec.UNSPECIFIED ) { //conform height to width
        height = width / aspectRatio;
    }
    else if( width / height > aspectRatio //too wide
            && ( widthMode == MeasureSpec.AT_MOST )
            ) { 
        width -= (width - height * aspectRatio);
    }
    else if( width / height < aspectRatio //too tall
            && ( heightMode == MeasureSpec.AT_MOST )
            ) { 
        height -= (height - width / aspectRatio);
    }

    int newWidthMeasure = MeasureSpec.makeMeasureSpec((int)width, MeasureSpec.AT_MOST);
    int newHeightMeasure = MeasureSpec.makeMeasureSpec((int)height, MeasureSpec.AT_MOST);
    measureChildren(newWidthMeasure, newHeightMeasure);

    setMeasuredDimension((int)width, (int)height);
}

我定义的纵横比在资源略微矩形的方面,但显然也有很多其他的方法可以做到这一点。

I'm defining the aspect ratio in terms of a nominal rectangle in resources, but obviously there are plenty of other ways to do this.

与感谢约瑟夫Villarey 谁指出我在 onMeasure(...) 摆在首位。

With thanks to Josephus Villarey who pointed me at onMeasure(...) in the first place.

这篇关于通知Android布局吹气多大&QUOT; WRAP_CONTENT&QUOT;应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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