Android: 中心闪屏图像
Android: Center splash screen image
我有一个带有启动画面的应用程序,启动画面在小型设备上看起来不错,但在大型平板电脑(模拟器)上看起来很糟糕。所以我将背景更改为 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>
请尝试以下
<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>
你可以使用相对布局,试试下面的代码..
<?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>
代码略有改动,因为它需要添加第二个元素以实现最佳匹配
<?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
实际上您在这里既不需要 LinearLayout
也不需要 RelativeLayout
,而是使用更轻量级的 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>
我正在为 activity 使用背景图片。首先在你的 styles.xml:
中创建一个主题
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
然后创建一个名为 background_splash.xml
的新绘图
<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>
然后将您的 activity 设置为在您的 AndroidManifest.xml 中使用此主题:
<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >
这对我有用。
我有一个带有启动画面的应用程序,启动画面在小型设备上看起来不错,但在大型平板电脑(模拟器)上看起来很糟糕。所以我将背景更改为 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>
请尝试以下
<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>
你可以使用相对布局,试试下面的代码..
<?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>
代码略有改动,因为它需要添加第二个元素以实现最佳匹配
<?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
实际上您在这里既不需要 LinearLayout
也不需要 RelativeLayout
,而是使用更轻量级的 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>
我正在为 activity 使用背景图片。首先在你的 styles.xml:
中创建一个主题<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
然后创建一个名为 background_splash.xml
的新绘图<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>
然后将您的 activity 设置为在您的 AndroidManifest.xml 中使用此主题:
<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >
这对我有用。