即使图像足够大,位图重力也会设置为不填充屏幕

问题描述 投票:3回答:4

我的Android项目中有一个RelativeLayout。这将它的背景设置为位图:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/splash2" />

splash2是一个大小为2560x1440像素的PNG图像。我没有直接将布局的背景设置为图像,因为默认的缩放模式(或重力)是fill,它可以拉伸图像以适应屏幕。使用center时,它应该从中心拍摄正确尺寸的图像并显示未缩放的图像。如果是垂直的1080x1920屏幕,它应该占用大块并将其置于布局中心。

但是,我有一个问题。图像比当今市场上的任何屏幕都要大。不过,凭借我的Nexus 7,它有一个1920x1080的屏幕,它的图像边缘。布局设置为全屏。图像垂直收缩。

我该如何解决?

android image android-layout bitmap android-drawable
4个回答
1
投票

尝试将其添加到位图:

android:gravity="fill_vertical"

这应该解决它。

附:对不起第一个答案,我现在编辑了。


1
投票

您可以使用android:gravity="fill"来覆盖垂直和水平方向


0
投票

对于飞溅图像尝试gravity =“center | fill”

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/black" />

    <item>
        <bitmap
            android:gravity="center|fill"
            android:src="@drawable/fondo" />
    </item>
</layer-list>

-1
投票

ImageView的比例类型centerCrop是我想要的。不幸的是我无法为位图指定此属性。我将我的初始屏幕布局更改为FrameLayout并添加了一个彼此重叠的ImageView和TextView。这样我就能达到我想要的效果。

<FrameLayout 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=".SplashScreen"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash2"
        android:scaleType="centerCrop" />

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/roadSignName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#FFF"
            android:gravity="center_vertical|center_horizontal"
            android:padding="10dp"
            android:text="My program"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000"
            android:textSize="40sp" />
    </RelativeLayout>
</FrameLayout>
© www.soinside.com 2019 - 2024. All rights reserved.