繁体   English   中英

如何在Android中调整背景图片大小以适合应用程序屏幕大小

[英]How to adjust background image size to fit the app screen size in android

我正在使用s4星系,并且我下载了以下壁纸壁纸http://www.sswallpaper.com/get/samsung-galaxy-s4-wallpapers/Keep-Running-1080x1920/595-2.jpg我试图使用此壁纸作为背景图片,但该图片不适合我的屏幕。 如何调整尺寸以适合应用程序的屏幕尺寸?

这是我的Xml代码:

      <?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="match_parent"
    android:background="@drawable/walking" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WELCOME"
        android:textSize="40dp"
        android:textColor="@color/textColor"
        android:textStyle="bold"
        android:typeface="serif"
        android:id="@+id/textView5"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp" />

    <TextView
        android:layout_width="189dp"
        android:layout_height="50dp"
        android:text="SKIP"
        android:textSize="20dp"
        android:textColor="@color/textColor"
        android:textStyle="bold"
        android:typeface="serif"
        android:gravity="center"
        android:id="@+id/skip"
        android:onClick="click"
        android:clickable="true"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/textView5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To skip the tutorial press SKIP at the bottom.Slide to the left to continue to tutorial page."
        android:id="@+id/textView3"
        android:textColor="@color/textColor"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp" />

</RelativeLayout>

我得到的图像的屏幕截图 屏幕截图

如果您不希望图像按比例缩放.....那么您应该使用..

android:scaleType="centerCrop"

编辑:

您应该将FrameLayout用于此类Layout,例如...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/walking"/>

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        .......

    </LinearLayout>

</FrameLayout>

选项1:

为不同的dpi创建不同的完美图像,并将它们放置在相关的可绘制文件夹中。 然后设置

android:background="@drawable/your_image

选项2:

添加单个大图像。 使用FrameLayout。 作为第一个孩子,添加一个ImageView。 在ImageView中设置以下内容。

android:src="@drawable/your_image"
android:scaleType = "centerCrop"

添加以下内容:

android:scaleType="fitXY"
android:src="@drawable/your_wallpaper_file"
   <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/walking"
    scaleType="fitXY" >

我尝试了一点改进的XML

<?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="match_parent"
    android:background="@drawable/walking" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:shadowColor="#000"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="1.0"
        android:text="WELCOME"
        android:textColor="#fff"
        android:textSize="40dp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/skip"
        android:layout_width="189dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_toStartOf="@+id/textView5"
        android:clickable="true"
        android:gravity="center"
        android:onClick="click"
        android:text="SKIP"
        android:textSize="20dp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:shadowColor="#000"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="1.0"
        android:text="To skip the tutorial press SKIP at the bottom.Slide to the left to continue to tutorial page." />

</RelativeLayout>

我的手机看起来不错

在此处输入图片说明

可能是您在xml第5行中使用了错误的drawable

android:background="@drawable/walking" 

在S4模拟器GenyMotion上测试:

在此处输入图片说明

暂无
暂无

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

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