如何定义的布局,将720p的工作,以及1080的屏幕? [英] How to define layout that will work on 720p as well as 1080p screens?

查看:153
本文介绍了如何定义的布局,将720p的工作,以及1080的屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我应该定义不同的布局不同的屏幕,请继续阅读。

I know I'm supposed to define different layouts for different screens, please read on.

我的工作将运行机器人的棒插入要么720p或1080p电视和单独留在家中表现出一定的幻灯片的应用程序。

I'm working on the app that will be run an "Android on the stick" plugged to either 720p or 1080p TV and left alone to show some "slide show".

我需要的东西如Android:layout_width =N%。我知道我可以用的LinearLayout做到这一点,但屏幕是相当复杂和如此深的嵌套气馁。我想坚持到RelativeLayout的。

I need something like android:layout_width="n%". I know I can do it with LinearLayout, but the screen is rather complex and such deep nesting is discouraged. I would like to stick to RelativeLayout.

我设计的布局,指定以像素为单位的指标,支持1080p设计,并把布局布局tvdpi或布局xhdpi希望的Andr​​oid将适当缩放降至720p。它不是。所有文物被夸大和重叠。

I have designed the layout specifying the metrics in pixels, designing for 1080p and put the layout to layout-tvdpi or layout-xhdpi hoping Android would scale it properly down to 720p. It does not. All artefacts are overblown and overlap.

有没有一种方法可以达到我想要什么?谢谢你。

Is there a way to achieve what I want? Thanks.

推荐答案

我在问题中提到,我工作的应用程序将显示720p或1080p的电视上。这使我有下列亵渎:所有视图大小/垫/利润率和文本大小在PX,没有DP,不界定SP。这样我可以控制的确切屏幕布局。
现在,相反的是文档使我相信,Android将不可以调整是在PX定义大小的布局元素。它只会做它可绘制或DP / SP。

As I mentioned in the question, the app I'm working on will be displayed on either 720p or 1080p TV. This allows me to commit the following blasphemy: All view sizes/paddings/margins and text sizes are defined in PX, not DP, not SP. This way I can control the exact screen layout. Now, contrary to what the documentation made me to believe, Android will not resize any layout element that has size defined in PX. It will only do it for drawables or DP/SP.

因此​​,我不得不调整我自己。

Thus I had to resize on my own.

我定义1080的布局。在Activity.onCreate()获取根布局并将它传递到:

I define the layouts for 1080p. in Activity.onCreate() get the root layout and pass it to:

private static final float RATIO_720_1080 = 720f/1080f;

/**
 * If the screen is 720p, the resources will resize
 * to fit the screen.
 * @param root Root view where the resources are found.
 */
public void resizeViews(ViewGroup root) {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    // only resize if 720p
    if (metrics.widthPixels != 1280) return;

    int childCount = root.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = root.getChildAt(i);

        // digg deep
        if (v instanceof ViewGroup) {
            resizeViews((ViewGroup)v);
        }

        // do the size
        ViewGroup.LayoutParams params = v.getLayoutParams();
        // keep the match_parent constants intact
        if (params.height > 0) {
            params.height = Math.round(params.height * RATIO_720_1080);
        }
        if (params.width > 0) {
            params.width = Math.round(params.width * RATIO_720_1080);
        }
        if (params instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) params;
            marginParams.setMargins(Math.round(marginParams.leftMargin * RATIO_720_1080),
                    Math.round(marginParams.topMargin * RATIO_720_1080),
                    Math.round(marginParams.rightMargin * RATIO_720_1080),
                    Math.round(marginParams.bottomMargin * RATIO_720_1080));
        }
        v.setLayoutParams(params);

        v.setPadding(Math.round(v.getPaddingLeft() * RATIO_720_1080),
                Math.round(v.getPaddingTop() * RATIO_720_1080),
                Math.round(v.getPaddingRight() * RATIO_720_1080),
                Math.round(v.getPaddingBottom() * RATIO_720_1080));

        // text size
        if (v instanceof TextView) {
            float size = ((TextView)v).getTextSize();
            size *= RATIO_720_1080;
            ((TextView)v).setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        }
    }
}

这篇关于如何定义的布局,将720p的工作,以及1080的屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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