Android活动背景图像

Android活动背景图像,android,background,android-manifest,xamarin.android,splash-screen,Android,Background,Android Manifest,Xamarin.android,Splash Screen,如何在活动主题/样式中使用背景图像 如果我使用颜色进行此操作: <style name="CustomTheme" parent="android:Theme.Light"> <item name="android:windowBackground">@color/custom_theme_color</item> </style> @颜色/自定义主题颜色 它工作正常,但如果我将该行替换为: <item name="androi

如何在活动主题/样式中使用背景图像

如果我使用颜色进行此操作:

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
</style>

@颜色/自定义主题颜色
它工作正常,但如果我将该行替换为:

<item name="android:windowBackground">@drawable/splash_image</item>
@drawable/splash\u图像
图像显示正确,但所有内容都被压缩到屏幕左上角的一个小块中。状态栏下方的阴影也被切断或弄乱


我使用的是Android的Mono,图像是一个有效的九补丁png。

我不使用主题或任何东西,所以我不确定这会有什么影响,但我的背景设置如下:

getWindow().setBackgroundDrawableResource(R.drawable.background_image);

在onCreate中,我也不使用主题,因此在活动的布局中我会添加主题

android:background="@drawable/splash_image"

我需要有一个看起来像我的应用程序的初始活动的飞溅图像,并遇到相同的问题。我很幸运使用了windowContentOverlay而不是windowBackground。drawable显示在状态栏下方,位置与实际布局完全相同。它已经在安卓2.2、安卓3.0和安卓4.1上运行

这是我的风格:

<style name="SplashTheme" parent="android:style/Theme.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@drawable/splash</item>    
</style>

真的
@可牵引/飞溅
My splash.xml drawable使用图层列表模拟我的用户界面标题:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--- Background; tile is broken on Android 2.2 so just have super wide background that will get clipped  -->
    <item>
        <bitmap android:gravity="top|left" android:src="@drawable/splash_background" />
    </item>

    <!--- Icon left justified -->
    <item>
        <bitmap android:gravity="top|left" android:src="@drawable/header_icon" />
    </item>

    <!--- Buttons/etc right justified  -->
    <item>
        <bitmap android:gravity="top|right" android:src="@drawable/splash_buttons"  />
    </item>
</layer-list>


我相信ActionBar也有一些内置的方法来处理这个问题,如果你的应用程序使用它的话。似乎有这样一个启动映像。

我的解决方案是将android:windowBackground更改为android:background

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="Theme.Splash" parent="android:Theme">
    <item name="android:background">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

@可牵引/飞溅
真的

您可以使用
android:gravity=“fill”
代替
android:gravity=“center”


注意:这取决于飞溅图像的形状和大小。在我的例子中,通过使用android:gravity=“fill”我的飞溅看起来很完美。
您可以在飞溅图像的类型上使用不同的重力属性。

谢谢各位,但我需要使用一个主题,因为布局内容来得太晚了。在android的mono中,启动时会有几秒钟的延迟。我需要在实际启动运行时之前显示图像。由于布局仅在运行时启动后应用,因此我必须使用主题,因为它会立即应用。没问题。我现在正在研究主题,因为我问了一个问题,所以如果我能解决这个问题,我会给你回复。代码的问题是,如果我们有一个“EditText”控件,当键盘出现时,背景图像正在压缩。是否可以将textview添加到splash.xml中?我需要显示一个带有徽标和文本(如连接)的启动屏幕…如何调整背景图像的大小,使其适合屏幕宽度并保持纵横比,使用此解决方案?我已经多年没有进行Android开发了。推荐新的StackOverflow问题。我不鼓励这种方法,因为在主题中定义它更干净。它允许Android在启动活动时立即显示,而不是在代码执行时看到短暂的闪烁。@PaulLammertsma我认为在视图显示之前执行onCreate时,您不会看到任何闪烁,在
onCreateView
中,对于阅读本文的每个人,请注意,您在windowBackground中用作自定义应用主题的可绘制文件将被完全解码加载到您的堆中。因为解码是在运行时完成的,所以即使是一个很小的映像(比如90KiB)也会导致分配90MiB甚至更多。这将以比光速更快的速度导致OutOfMemory问题。:)有关更多信息,请阅读本文如何正确设置初始屏幕:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="translucent">

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