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

标签 android android-layout

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

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

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

我设计了以像素为单位指定指标的布局,为 1080p 设计并将布局设置为 layout-tvdpi 或 layout-xhdpi,希望 Android 能够将其适本地缩小到 720p。它不是。所有 Artifact 都被夸大和重叠。

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

最佳答案

正如我在问题中提到的,我正在开发的应用程序将在 720p 或 1080p 电视上显示。这让我犯了以下亵渎罪:所有 View 大小/填充/边距和文本大小都是在 PX 中定义的,而不是 DP,不是 SP。这样我就可以控制确切的屏幕布局。 现在,与文档让我相信的相反,Android 将不会调整任何在 PX 中定义了大小的布局元素的大小。它只会对可绘制对象或 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);
        }
    }
}

关于android - 如何定义适用于 720p 和 1080p 屏幕的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819348/

相关文章:

android - 由于 setOnKeyListener,Edittext 不接受数字输入

android - 更新 Firestore 中的文档

Android-DirectionalViewPager : NoClassDefFoundError: com. orientalviewpager.DirectionalViewPager$DataSetObserver

java - 如何获得中性按钮的标准颜色

安卓应用权限

android - Flutter 应用在​​ Release模式下显示灰屏,但在 Debug模式下工作正常 'DiagnosticsProperty<void>' 实例

android - RecyclerView 未预览

android - 可扩展的 ListView 或可点击的线性布局

android - 自定义 Sipdroid

Android onClickListener 在复合 View 中不起作用