我有一个带有启动画面的应用程序,并且启动画面在小型设备上看起来不错,但看起来在大平板电脑(模拟器)中搞砸了.所以我改变了背景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>
Run Code Online (Sandbox Code Playgroud)
代码略有变化,因为它需要添加第二个元素才能最适合
<?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>
Run Code Online (Sandbox Code Playgroud)
添加 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>
Run Code Online (Sandbox Code Playgroud)
我正在使用背景图片进行活动。首先在您的styles.xml中创建一个主题:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后使用名称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>
Run Code Online (Sandbox Code Playgroud)
然后将您的活动设置为在AndroidManifest.xml中使用此主题:
<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >
Run Code Online (Sandbox Code Playgroud)
这对我有用。
| 归档时间: |
|
| 查看次数: |
8686 次 |
| 最近记录: |