簡體   English   中英

如何在Android中的多種類型的設備上以固定的長寬比背景進行項目布局?

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

我們要做的是:

在此處輸入圖片說明

在電視屏幕上顯示子布局。 電視布局的寬度為match_parent。

我試過的

<?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>

結果:

在此處輸入圖片說明

您的比例看起來不錯,您需要兩個孩子都具有相同的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>

您可能還需要一種布局來包裝它們,具體取決於您的UI設計

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM