Android Theme and the solution of starting black screen

  • 2021-10-11 19:27:38
  • OfStack

Preface

This article mainly introduces the related content about Android Theme and solving the problem of starting black screen, and shares it for everyone's reference and study. The following words are not much to say, let's take a look at the detailed introduction

1. Modify AndroidManifest. xml to set the global Theme of App or the interface Theme of Activity


<application
 android:allowBackup="true"
 android:icon="@drawable/ipod_icon"
 android:label="@string/app_name"
 android:launchMode="singleTask">
<!-- iPod Main interface  -->
<activity
 android:name="com.apical.apicalipod.IPodMainActivity"
    <!--  Use the style defined above  mythou-->
 android:theme="@style/Theme.AppStartLoad"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>
//......
</application>

You can add the above custom styles in Activity. In addition, it is possible to add in Application, and it is a global effect.

Custom Theme is placed in/res/values/styles. xml. If you don't have this file, add one by yourself.

If there are multiple Activity switches, there may be a short black screen problem in the middle. The reason is that the Activity needs to initialize the loading data when starting. If you want to avoid this situation, you can add the above style in the Activity you switch.

The above two styles can avoid black screen. You can actually test your program for 1 effect.

This only avoids the black screen, but if your program initializes slowly, it will still give people the feeling that the program starts slowly. You need to optimize the program initialization process yourself.


android:theme="@android:style/Theme.Dialog" //Activity Display as dialog mode 
android:theme="@android:style/Theme.NoTitleBar" // Do not display the application title bar 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" // Do not display the application title bar, and full screen 
android:theme="Theme.Light " // The background is white 
android:theme="Theme.Light.NoTitleBar" // White background without title bar 
android:theme="Theme.Light.NoTitleBar.Fullscreen" // White background, no title bar, full screen 
android:theme="Theme.Black" // Black background 
android:theme="Theme.Black.NoTitleBar" // Black background without title bar 
android:theme="Theme.Black.NoTitleBar.Fullscreen" // Black background, no title bar, full screen 
android:theme="Theme.Wallpaper" // Use the system desktop as the application background 
android:theme="Theme.Wallpaper.NoTitleBar" // Use the system desktop as the application background and have no title bar 
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" // Use system desktop as application background, no title bar, full screen 
android:theme="Theme.Translucent" // Transparent background 
android:theme="Theme.Translucent.NoTitleBar" // Transparent background without title 
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" // Transparent background with no title, full screen 
android:theme="Theme.Panel " // Panel style display 
android:theme="Theme.Light.Panel" // Flat panel style display 

Theme and Style

In addition to Theme, there are Style in Android. For example, the following is an Style configured with workspace in Launcher


<style name="WorkspaceIcon">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">match_parent</item>
  <item name="android:layout_gravity">center</item>
  <item name="android:gravity">center_horizontal</item>
  <item name="android:singleLine">true</item>
  <item name="android:ellipsize">marquee</item>
  <item name="android:textSize">12sp</item>
  <item name="android:textColor">#FFF</item>
  <item name="android:shadowRadius">2.0</item>
  <item name="android:shadowColor">#B0000000</item>
 </style>

Style can be understood as a set of attributes, which is convenient for different View settings. When we use Style in View, it is the same application method as using Theme. So what's the difference between Style and Theme?

The difference between the two is listed below:

Styles are used in separate View, such as Button, TextView, etc.

Themes are used in the whole application or an Activity through the sum in AndroidManifest. xml, and themes have a global impact on the whole application or an Activity.

If an application uses a theme and the view under the application also uses a style, when the theme conflicts with the style attribute, the style takes precedence over the theme.

The above is to solve the problem of flash black screen when the program starts through Theme, and explain Theme and Style. Through Theme configuration, you can actually make a welcome page. However, we all hope that the faster the program starts, the better, so we still need to optimize our own programs.

2. Solve the problem of starting black screen:

Reasons for occurrence:

1 reason loading onCreate method executing method loading data is time consuming, and the interface will be displayed only after running onCreate and onResume

The main reason for flashing the black screen is that when we start Activity, we need to run onCreate and onResume before displaying the interface. That is to say, it will not be displayed until 1 data is processed. According to this idea, can I avoid black screen by minimizing the initialization work? The fact is, even if you don't do anything, onCreate will still flash a black screen, because it takes 1 time to initialize the parsing interface. Here are the solutions:

Resolve:

Set the background image Theme


<style name="Theme.AppStartLoad" parent="android:Theme"> 
 <item name="android:windowBackground">@drawable/ipod_bg</item> 
 <item name="android:windowNoTitle">true</item> 
</style>

// ps:  If you want to set this still page to full screen, add 1 Sentence :  
// <item name="android:windowFullscreen">true</item>  You can 

//2 Setting Transparency Theme
<style name="Theme.AppStartLoadTranslucent" parent="android:Theme"> 
 <item name="android:windowIsTranslucent">true</item> 
 <item name="android:windowNoTitle">true</item> 
</style>

Above, I defined two kinds of Theme, and the first Theme is to set up a background map. When the program starts, first display this background image to avoid a black screen. The second type of Theme is to set the style to transparent. After the program starts, it will not be black but transparent, and it will be displayed once when the interface is initialized.

Let's talk about the advantages and disadvantages of the two methods:

• ES 119EN1 program starts quickly, and the interface displays the background picture first, and then refreshes other interface controls. Give people the feeling of refreshing out of sync.

• ES 123EN2 gives people the feeling that the program starts slowly, and the interface is brushed out once, refreshing and synchronizing.

Summarize


Related articles: