1

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

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

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

我设计了以像素为单位指定指标的布局,针对 1080p 进行设计,并将布局放入 layout-tvdpi 或 layout-xhdpi,希望 Android 能够将其适当地缩小到 720p。它不是。所有的人工制品都被夸大和重叠。

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

4

2 回答 2

1

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.

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);
        }
    }
}
于 2013-11-07T19:55:37.853 回答
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>
于 2013-11-06T18:10:16.500 回答