Android of Theme and Style Style

  • 2020-05-17 06:27:14
  • OfStack

A few days ago, Boss responded that every time the machine started the program, it would flash a black screen, which this customer did not accept. No way, can only think of how to solve, finally found the following method. The main reason for the black screen is that when we start Activity, we need to run onCreate and onResume to display the interface. In other words, you need to process some data before it will be displayed. According to this thinking, can I minimize the initialization work so as to avoid the black screen? The truth is, even if you don't do anything with onCreate, you're still going to flash a black screen, because it takes a certain amount of time to unpack the interface. Here's the solution:
1. Customize Theme


 Setting the background Theme
<style name="Theme.AppStartLoad" parent="android:Theme">  
    <item name="android:windowBackground">@drawable/ipod_bg</item>  
    <item name="android:windowNoTitle">true</item>  
</style>
//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 types of Theme. The first type, Theme, is to set a background image. When the program starts, display the background image first to avoid a black screen. The second Theme is to set the style to transparent, so that the screen will be transparent instead of black when the program is started, and it will be displayed once the interface is initialized. Here are the pros and cons of the two approaches:
The & # 8226; Theme1 program starts quickly, the interface first displays the background image, and then refreshes other interface controls. Give a person refresh out of sync feeling.
The & # 8226; Theme2 gives people the feeling of slow program startup, the interface is brushed out once, and the synchronization is refreshed.

2. Modify AndroidManifest. xml
In order for Theme above to work, we need to set 1 Theme of Activity

<application
    android:allowBackup="true"
    android:icon="@drawable/ipod_icon"
    android:label="@string/app_name"
    android:launchMode="singleTask">
<!-- iPod The main interface  -->
<activity
    android:name="com.apical.apicalipod.IPodMainActivity"
  <!--  Use the styles 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>

The & # 8226; You can add the above custom style to Activity. In addition, it is also possible to add Application, and it is a global effect.
The & # 8226; Custom Theme in/res/values/styles xml inside. If you don't have this file, just add one yourself.
The & # 8226; If there are multiple Activity switches, there may also be a temporary black screen problem in the middle. The reason is also that Activity needs to be initialized to load the data when it is started. To avoid this, you can add the above style to the Activity you switch.
The & # 8226; Both styles avoid a black screen. You can actually test 1 under your program to select 1 effect.
The & # 8226; This just avoids the black screen, but if you have a slow startup, it still feels like a slow startup. You need to optimize the program initialization process yourself.

3. Theme properties explanation

android:theme="@android:style/Theme.Dialog" //Activity Display in dialog mode 
android:theme="@android:style/Theme.NoTitleBar" // The application title bar is not displayed 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" // Do not display the application title bar, and the full screen 
android:theme="Theme.Light " // The background is white 
android:theme="Theme.Light.NoTitleBar" // The white background has no 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" // There is no title bar on a black background 
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 no title bar 
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" // Use the system desktop as the application background, no title bar, full screen 
android:theme="Theme.Translucent" // Transparent background 
android:theme="Theme.Translucent.NoTitleBar" // The transparent background has no 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 display 

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

 <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 Settings of View. When we use Style in View, it is the same application method as when we use Theme. So what's the difference between Style and Theme?

Here are the differences:
The & # 8226; Styles are used in separate View, such as Button, TextView, etc

The & # 8226; Theme through AndroidManifest.xml < application > and < activity > When applied to an entire application or to an Activity, the theme has a global impact on the entire application or to an Activity.

The & # 8226; If an application USES a theme and view under the application also USES a style, then when the theme conflicts with the style properties, the style takes precedence over the theme.

The above is to solve the problem of launching the app with a black screen through Theme, and explain Theme and Style. You can also make a welcome page through the configuration of Theme. However, we all want our programs to start as fast as possible, so we need to optimize our programs.


Related articles: