安卓:如何居中显示启动界面图片

11

我的应用程序有一个启动画面,在小设备上看起来不错,但在大型平板电脑(模拟器)上看起来很糟糕。因此,我将背景更改为wrap_content。但是它依然靠在屏幕的一侧,有人可以告诉我如何将背景图像居中吗?这是我的splash.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/splash">
</LinearLayout>
5个回答

12

我正在为我的活动使用背景图像。首先在你的styles.xml中创建一个主题:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

然后创建一个名为background_splash.xml的新drawable。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorWhite"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/logo_splash"/>
    </item>
</layer-list>

然后在你的 AndroidManifest.xml 文件中设置你的活动使用这个主题:

<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >

这对我起作用了。


FYI,这是正确的做法。您的背景颜色会伸展到与屏幕尺寸匹配,然后您的徽标将在屏幕中心而不会变形或扭曲。不确定 @user924 说的代码错误是什么,但这个例子就是我遵循的,它效果很好。我对它进行了一些微调,以便在许多不同的设备上(旧/新平板电脑、手机等)得到完美的效果,但这为工作打下了坚实的基础。谢谢! - Joshua Pinter

9

在这里,实际上不需要使用LinearLayoutRelativeLayout,而是应该使用更轻量级的FrameLayout

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

    <ImageView
        android:src="@drawable/splash"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

5

代码略作改动,需要添加第二个元素以获得更好的匹配

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2A7800"<---- optional background color to fit the image. to make it blend in
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"<--- centers
        android:adjustViewBounds="true"
        android:scaleType="fitXY"<--- makes sure the image fits into the layout my matching the screen size.

        android:src="@drawable/splash" />
</LinearLayout>

添加ImageView是最佳实践,因为它在性能上优于使用android:background并设置图像。我通过一个ScrollView发现了这一点,其主父级设置了android:background以显示图像,这使得ScrollView变得非常缓慢。

此外,使用这种方法不需要特定的布局,您可以使用RelativeLayout、FrameLayout、LinearLayout等任何可用的布局。唯一的例外可能是ListView、GridView。


3
如果您有一张大小与屏幕不同的图像,您可以使用 android:scaleType="centerInside" 而不是 fixXY 来进行缩放和居中处理,使其更加美观。 - Aaron Tribou

1
请尝试以下内容。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash">
</LinearLayout>

不同的启动画面可用于绘制不同的可视化效果。 - Halumz - Mashuk Sadman

1
你可以使用相对布局,尝试以下代码...
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接