Android背景图像适合屏幕没有拉伸

时间:2015-03-04 06:04:56

标签: android background

我开发了一个Android应用程序。我使用android:background属性将图像作为背景。但是当它在方形显示器上显示时,图像会保持伸展状态。 如何使Android背景适合屏幕而不拉伸? 我看过很多应用程序的图像或视频都没有拉伸,但是填满了Android手机的整个屏幕。

我的xml代码在下面给出

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity"
    android:background="@drawable/Splash" >

</RelativeLayout>

4 个答案:

答案 0 :(得分:7)

您可以尝试使用ImageView,而不是将其设置为RelativeLayout

的背景

我修改了你的代码,看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/Splash"/>
</RelativeLayout>

此解决方案的唯一问题是图像可能会在某些设备上的部件处被切断。要解决此问题,您可以为相关设备创建一些具有不同宽高比的图像,并将它们放入相关的drawable-xxxx文件夹中。

另请参阅this主题,了解可能更符合您要求的其他解决方案。

答案 1 :(得分:0)

你需要多个图像用于不同的显示器 Supporting Multiple Screens

答案 2 :(得分:0)

在Android中,为了支持多个屏幕,您必须根据屏幕分辨率和屏幕尺寸添加不同的图像。

有关详细信息,请访问以下链接:

  1. Drawable folders in res folder?
  2. http://developer.android.com/guide/practices/screens_support.html

答案 3 :(得分:0)

您可以使用FrameLayout并将ImageView作为背景

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgPlaylistItemBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:maxHeight="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/img_dsh" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>

</FrameLayout>
相关问题