Android:在启动的 Activity 上设置窗口背景

标签 android background android-layout

所以我读过 Romain Guy 的 blog post关于设置窗口背景和感知性能,并试图效仿。这是一个如此简单的解决方案,不知道为什么我不能让它工作,但 Activity 只是拒绝接收定向背景。

我有一个 ListView,onListItemClick 会启动一个新的 Activity,它需要 3-5 秒才能完全加载。在用户等待时,我想绘制一个 windowBackground 以便他们在 Activity 真正准备好之前“看到” Activity 。这是我的代码:

已启动 Activity 的 AndroidManifest fragment :

<activity 
        android:name=".activity.EditorActivity"
        android:screenOrientation="portrait"
        android:windowBackground="@drawable/background_editor">

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>

最后,在 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" />

editor_bg 是一个 .png 文件,位于可绘制文件夹中。

最终结果是 EditorActivity 被启动,我看到的只是默认的黑色背景,“编辑器”文本显示为白色(我添加了它以测试 XML 文件是否正确加载。

我还尝试通过 android:background="@android:color/transparent"将 FrameLayout 和 TextView 的背景设置为透明,认为它们可能默认为黑色背景,但没有运气。

已经好几天了,我确定我错过了一些简单的东西......我在这里犯了什么明显的错误?

最佳答案

尝试在以编程方式启动新 Activity 之前设置窗口背景。

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

关于Android:在启动的 Activity 上设置窗口背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837889/

相关文章:

css - body 背景问题

android - 选择时更改操作背景颜色(ActionBarSherlock)

Android缩放动画到指定尺寸

android - 如何在android listview或gridview中动态添加图像/图像按钮

android - 调用服务的 Activity 或 Receiver 的名称

java - 如何关闭充气层?

css - 背景图片在IE7/8中消失

android - RecyclerView onBindviewholder Dialog clicklistner Animation

Android - MEDIA ERROR UNKNOWN -1 通常意味着什么

android - Ionic 5 Android 应用程序因使用 Paypal 而不是应用内计费而被拒绝