【问题标题】:How to adjust background image size to fit the app screen size in android如何调整背景图像大小以适应 android 中的应用程序屏幕大小
【发布时间】:2016-01-24 08:29:20
【问题描述】:

我使用的是 s4 Galaxy,我下载了以下壁纸 wallpaper 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>

我得到的图片截图

【问题讨论】:

  • 您是否考虑过使用 fill_parent 而不是 match_parent?这次尝试将它与 src 结合起来。

标签: android android-imageview


【解决方案1】:

如果你不希望你的图像缩放.....那么你应该使用..

android:scaleType="centerCrop"

编辑:

这种布局你应该使用 FrameLayout,像这样...

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

【讨论】:

  • 也对此进行了测试。我不知道为什么它不起作用。 :( 无论如何我只是下载另一个图像大小。
【解决方案2】:

选项 1:

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

android:background="@drawable/your_image

选项 2:

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

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

【讨论】:

    【解决方案3】:
       <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/walking"
        scaleType="fitXY" >
    

    【讨论】:

    • 您使用的是哪个小部件?
    【解决方案4】:

    添加以下内容:

    android:scaleType="fitXY"
    android:src="@drawable/your_wallpaper_file"
    

    【讨论】:

    • 你试过设置图像视图android:src而不是背景吗?
    • 能否请您发布结果的屏幕截图以及您修改图像视图的任何代码。
    • 顺便说一句,分享您的 xml 整个代码,因为可能问题不在 imageview 及其父级(例如 LayoutView)中
    • 您好,我粘贴了整个 xml 和屏幕截图。
    • 我也测试过。它没有用。 :( 无论如何我下载了另一个图像大小。
    【解决方案5】:

    我尝试了您的 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 中使用了错误的可绘制对象

    android:background="@drawable/walking" 
    

    在 S4 模拟器 GenyMotion 上测试:-

    【讨论】:

    • 你也在用s4吗?
    • 查看我的编辑我已经在 GenyMotion S4 模拟器上测试过。
    猜你喜欢
    • 2011-04-17
    • 2011-02-03
    • 2018-01-11
    • 2020-08-26
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多