简体   繁体   English

如何在Android中的多种类型的设备上以固定的长宽比背景进行项目布局?

[英]How to make an item layout with a fixed aspect ratio background on multiple types of devices in Android?

What we want to make: 我们要做的是:

在此处输入图片说明

Display a sub layout on the screen of the TV. 在电视屏幕上显示子布局。 The width of the TV layout is match_parent. 电视布局的宽度为match_parent。

What I Tried: 我试过的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:src="@drawable/bg_tv_left" />
    <include
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="13dp"
        android:layout_marginLeft="13dp"
        layout="@layout/content" />
</RelativeLayout>

The Result: 结果:

在此处输入图片说明

Your ratio looks good, you need both child have the same layout_width and layout_height to get the perfect position, try this one: 您的比例看起来不错,您需要两个孩子都具有相同的layout_width和layout_height才能获得理想的位置,请尝试以下方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:src="@drawable/bg_tv_left" />
<!-- adjust the margin to get the best positioning -->
<include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="13dp"
        android:layout_marginLeft="13dp"
        layout="@layout/content" />
</RelativeLayout>

You may need one more layout to wrap them, depend on your UI design 您可能还需要一种布局来包装它们,具体取决于您的UI设计

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM