如何定义适用于720p和1080p屏幕的布局?

时间:2013-11-06 18:00:15

标签: android android-layout

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

我正在开发一款应用程序,该应用程序将运行一个“Android on the stick”插入720p或1080p电视并单独留下以显示一些“幻灯片放映”。

我需要像android这样的东西:layout_width =“n%”。我知道我可以用LinearLayout做到这一点,但屏幕相当复杂,不鼓励这种深度嵌套。我想坚持使用RelativeLayout。

我设计了以像素为单位指定度量的布局,设计为1080p,并将布局设置为layout-tvdpi或layout-xhdpi,希望Android能够将其正确缩放到720p。它不是。所有人工制品都过分夸张和重叠。

有没有办法实现我想要的?感谢。

2 个答案:

答案 0 :(得分:1)

正如我在问题中所提到的,我正在处理的应用程序将显示在720p或1080p电视上。这允许我提交以下亵渎:所有视图大小/填充/边距和文本大小都在PX中定义,而不是DP,而不是SP。这样我就可以控制确切的屏幕布局。 现在,与文档让我相信的相反,Android将调整任何具有在PX中定义的大小的布局元素。它只会用于drawables或DP / SP。

因此我必须自己调整大小。

我定义了1080p的布局。在Activity.onCreate()中获取根布局并将其传递给:

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

答案 1 :(得分:0)

您可以使用如下的布局文件。请注意父布局中的layout_sum属性以及每个子项中的layout_weight属性。第一个孩子将占父母的70%,第二个孩子占30%。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70"
        android:text="A"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:text="B"/>
</LinearLayout>
相关问题