1

我目前有一個針對我的啓動畫面的活動。以下是我將主題應用到活動方式:覆蓋由主題設置的活動背景

<application 
    ... 

    <activity 
     android:name=".activities.SplashActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:screenOrientation="portrait" 
     android:theme="@style/SplashTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

SplashTheme定義如下:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="android:background">@drawable/splash_background</item> 
</style> 

splash_backround.xml定義如下:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <bitmap 
     android:src="@drawable/login_background" 
     android:tileMode="disabled" /> 
</item> 
<item> 
    <bitmap 
     android:src="@drawable/login_siren" 
     android:tileMode="disabled" 
     android:gravity="bottom|center_horizontal" /> 
</item> 
<item android:top="120dp"> 
    <bitmap 
     android:src="@drawable/splash_welcome" 
     android:tileMode="disabled" 
     android:gravity="top|center_horizontal" /> 
</item> 

我不是在我的SplashActivity類的onCreate()中調用setContentView(),所以顯示的東西d是SplashTheme設置的。

如果在Application.onCreate()上加載失敗,我想要顯示AlertDialog的情況出現了。啓動畫面在我使用它創建,創建並顯示AlertDialog時顯示。但是,當我顯示AlertDialog時,它會從Activity的主題中分配背景。即使我爲AlertDialog(通過android:alertDialogTheme或android:alertDialogStyle)定義了明確定義不同背景的自定義樣式,它也會被活動定義的背景取代。其結果是,我已經改變SplashTheme以下幾點:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="android:windowBackground">@drawable/splash_background</item> 
</style> 

現在,這釋放了警告對話框背景的控制,因爲我沒有明確設置的android:背景在我的活動主題。但是,窗口背景並不考慮狀態欄(即背景也適用於狀態欄下方)。因此,當啓動畫面切換到在其佈局中使用相同背景的下一個活動(由於考慮到狀態欄)時,我會有點跳躍。所以,這個解決方案仍然不能按我的想法工作。

如果我使用原始的SplashTheme(我定義的android:背景),有沒有辦法來覆蓋AlertDialog的主題定義的android:背景?

回答

0

一旦應用活動的主題就不可能改變。

有沒有辦法改寫AlertDialog的主題定義的android:background

AlertDialog有一個構造函數,它接受themeResId作爲輸入。從docs of AlertDialog (Context context, int themeResId)

創建使用明確主題資源的警報對話框。

創建自定義主題,覆蓋android:background屬性,然後使用創建AlertDialog新創建的主題:

Dialog dialog = new AlertDialog(context, R.style.my_dialog_theme);