【问题标题】:Android: Setting Window Background on launched ActivityAndroid:在启动的活动上设置窗口背景
【发布时间】:2011-01-29 16:13:15
【问题描述】:

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

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

已启动 Activity 的 AndroidManifest sn-p:

<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 的背景设置为透明,认为它们可能默认为黑色背景,但没有运气。

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

【问题讨论】:

    标签: android background android-layout


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,并为此浪费了一天的时间。我们无法在 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>
      

      现在我创建的主题如下:

      <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>
      

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

      <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
          android:antialias="true"
          android:dither="true"
          android:gravity="fill"
          android:src="@drawable/splash_screen" />
      

      【讨论】:

        【解决方案3】:

        如果我在styles.xml 中的主题样式中声明它对我有用:

        <?xml version="1.0" encoding="utf-8"?>
        <resources xmlns:android="http://schemas.android.com/apk/res/android"   
        xmlns:app="http://schemas.android.com/apk/res-auto">
        
            <style name="CustomTheme" parent="android:Theme.Holo.NoActionBar">
                <item name="android:windowBackground">@color/bg_evening</item>
            </style>
        
        </resources>
        

        在清单中我将此主题设置为我的应用程序:

        <application
            android:name="com.my_app.pack"
            android:theme="@style/CustomTheme" >
        ...
        

        或参加我的一项活动:

        <activity
            android:name="..."
            android:theme="@style/CustomTheme">
        ...
        

        您可以声明多个主题样式并将它们中的任何一个添加到您的任何活动中,这将覆盖应用程序的主题集

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-14
          相关资源
          最近更新 更多