Android:在启动的Activity上设置Window Background

Pau*_*ega 23 android background android-layout

所以我已经阅读了Romain Guy 关于设置Window背景和深刻表现的博客文章,并试图模仿它.这是一个简单的解决方案,并不确定为什么我不能让这个工作,但活动只是拒绝接收定向背景.

我有一个ListView,onListItemClick启动一个新的Activity,需要3-5秒才能完全加载.当用户在等待时,我想绘制一个windowBackground,以便他们在实际准备好之前"看到"该活动.这是我的代码:

针对已启动活动的AndroidManifest代码段:

<activity 
        android:name=".activity.EditorActivity"
        android:screenOrientation="portrait"
        android:windowBackground="@drawable/background_editor">
Run Code Online (Sandbox Code Playgroud)

EditorActivity的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView 
    android:id="@+id/editor"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Editor" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

最后,drawife在Manifest中设置,background_editor.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/editor_bg"
    android:tileMode="repeat" />
Run Code Online (Sandbox Code Playgroud)

editor_bg是一个位于drawable文件夹中的.png文件.

最终结果是EditorActivity被启动,我看到的是默认的黑色背景,其中"Editor"文本显示为白色(我添加了以测试XML文件是否正确加载).

我也尝试通过android:background ="@ android:color/transparent"将FrameLayout和TextView的背景设置为透明,想想也许他们默认为黑色背景,但没有运气.

已经很长一段时间了,我确信我错过了一些简单的事情...我在这里犯的任何明显的错误?

Zar*_*kka 52

尝试以编程方式启动新活动之前设置窗口背景.

getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);
Run Code Online (Sandbox Code Playgroud)


Gem*_*Gem 5

我遇到了同样的问题,并在上​​面浪费了我的一天。我们无法在 Java 中设置主题,因为在我的情况下,控件很晚才到达我的启动活动的 onCreate。直到那个时候黑屏保持可见。

这给了我线索,必须从我们在清单中指定的主题管理窗口。

我有清单:

<activity
        android:name=".main.activities.SplashScreen"
        android:theme="@style/Splash"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

现在我创建的主题如下:

<style name="Splash" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/green_09</item>
    <item name="colorPrimary">@color/green_09</item>
    <item name="windowActionBar">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在包含位图资源的可绘制资源中飞溅,我必须稍微调整一下以使其看起来完美而不是拉伸和居中:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:gravity="fill"
    android:src="@drawable/splash_screen" />
Run Code Online (Sandbox Code Playgroud)