Switch UI between Android Theme and Style

Source: Internet
Author: User

There are many methods for skin replacement for android. If you do not need to make an open interface, but your software only supports skin replacement or development at the framework layer, You need to modify the skin of the app according to the system settings, theme can be used for implementation. Its advantage is in terms of maintenance and expansion, which is easy to implement. You only need to use the xml file to define the required Skin in advance and add a small amount of code to the project.

1. Add a variable. xml variables are generally defined in the attrs. xml file, which is located in the res/values directory and need to be manually added. These variables can be defined as Drawable, color, dim, and other types. As follows:

 
                                         
 

2. assign values to these variables in the res/values/style. xml file. Different styles can assign different values to these variables. The value type is affected by attrs, as shown below:

            

3. Implement a basic class to call these styles. Other activities use BaseActivity.

public class BaseActivity extends Activity {public int mTheme = R.style.AppTheme_Default;@Overrideprotected void onCreate(Bundle savedInstanceState) {/*if (savedInstanceState == null) {mTheme = PreferenceHelper.getTheme(this);} else {mTheme = savedInstanceState.getInt("theme");}*/setTheme(mTheme);super.onCreate(savedInstanceState);}@Overrideprotected void onResume() {super.onResume();//if (mTheme != PreferenceHelper.getTheme(this)) {//reload();//}}protected void reload() {Intent intent = getIntent();overridePendingTransition(0, 0);intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);finish();overridePendingTransition(0, 0);startActivity(intent);}}

You can also write a class for calling the activity that requires skin replacement, for example:

    Public class Utils
  1. {Private static int sTheme;
  2. Public final static int THEME_DEFAULT = 0;
  3. Public final static int THEME_WHITE = 1; public final static int THEME_BLUE = 2;
  4. /**
  5. * Set the theme of the Activity, and restart it by creating a new Activity * of the same type.
  6. */Public static void changeToTheme (Activity activity, int theme)
  7. {STheme = theme;
  8. Activity. finish ();
  9. Activity. startActivity (new Intent (activity, activity. getClass ()));}
  10. /** Set the theme of the activity, according to the configuration .*/
  11. Public static void onActivityCreateSetTheme (Activity activity ){
  12. Switch (sTheme ){
  13. Default: case 1:
  14. Activity. setTheme (R. style. Theme_Translucent); break;
  15. Case 2: activity. setTheme (R. style. Theme_Translucent2 );
  16. Break; case 3:
  17. Activity. setTheme (R. style. Theme_Transparent); break;
  18. }}
  19. }

    Appendix: attrs. xml usage:



    1. reference: refer to a resource ID.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "42dip"

    Android: layout_height = "42dip"

    Android: background = "@ drawable/image ID"

    />


    2. color: color value.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "42dip"

    Android: layout_height = "42dip"

    Android: textColor = "#00FF00"

    />


    3. boolean: boolean value.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "42dip"

    Android: layout_height = "42dip"

    Android: focusable = "true"

    />

    4. dimension: dimension value.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "42dip"

    Android: layout_height = "42dip"

    />


    5. float: floating point value.

    (1) attribute definition:

    (2) attribute usage:

    Android: fromalphi = "1.0"

    Android: toAlpha = "0.7"

    />

    6. integer: integer value.

    (1) attribute definition:

    (2) attribute usage:

    Xmlns: android = "http://schemas.android.com/apk/res/android"

    Android: drawable = "@ drawable/image ID"

    Android: Required Tx = "50%"

    Android: Ty = "50%"

    Android: framesCount = "12"

    Android: frameDuration = "100"

    />

    7. string: string.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "fill_parent"

    Android: layout_height = "fill_parent"

    Android: apiKey = "0jokq80od1jl9c6haja99ugxcri2cgjko_bc_g"

    />


    8. fraction: percentage.

    (1) attribute definition:


    (2) attribute usage:

    Xmlns: android = "http://schemas.android.com/apk/res/android"

    Android: interpolator = "@ anim/animation ID"

    Android: fromDegrees = "0"

    Android: toDegrees = "360"

    Android: Required Tx = "200%"

    Android: Ty = "300%"

    Android: duration= "5000"

    Android: repeatMode = "restart"

    Android: repeatCount = "infinite"

    />


    9. enum: enumeration value.

    (1) attribute definition:

    (2) attribute usage:

    Xmlns: android = "http://schemas.android.com/apk/res/android"

    Android: orientation = "vertical"

    Android: layout_width = "fill_parent"

    Android: layout_height = "fill_parent"

    >


    10. flag: bitwise OR operation.

    (1) attribute definition:


    (2) attribute usage:

    Android: name = ". StyleAndThemeActivity"

    Android: label = "@ string/app_name"

    Android: windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

    Note:

    You can specify multiple types of values when defining attributes.

    (1) attribute definition:

    (2) attribute usage:

    Android: layout_width = "42dip"

    Android: layout_height = "42dip"

    Android: background = "@ drawable/image ID | #00FF00"

    />




    Attribute values of style in android

    AndroidTheme styles defined by the Platform:

    Android: theme = "@ android: style/Theme. Dialog" shows an Activity as a Dialog box

    ? Android: theme = "@ android: style/Theme. NoTitleBar" does not display the application title bar
    ? Android: theme = "@ android: style/Theme. NoTitleBar. Fullscreen" does not display the application title bar, and is displayed in full screen.

    ? Android: theme = "@ android: style/Theme. Light": the background is white.
    ? Android: theme = "@ android: style/Theme. Light. NoTitleBar" the white background does not have a title bar.
    ? Android: theme = "@ android: style/Theme. Light. NoTitleBar. Fullscreen" white background, no title bar, full screen

    ? Android: theme = "@ android: style/Theme. Black" background Black
    ? Android: theme = "@ android: style/Theme. Black. NoTitleBar" the Black background does not have a title bar.
    ? Android: theme = "@ android: style/Theme. Black. NoTitleBar. Fullscreen" Black background, no title bar, full screen

    ? Android: theme = "@ android: style/Theme. Wallpaper" use the system desktop as the background of the application
    ? Android: theme = "@ android: style/Theme. Wallpaper. NoTitleBar" uses the system desktop as the background of the application without a title bar.
    ? Android: theme = "@ android: style/Theme. Wallpaper. NoTitleBar. Fullscreen" uses the system desktop as the background of the application, without a title bar, full screen

    ? Android: theme = "@ android: style/Translucent" Translucent Effect
    ? Android: theme = "@ android: style/Theme. Translucent. NoTitleBar" Translucent and no title bar
    ? Android: theme = "@ android: style/Theme. Translucent. NoTitleBar. Fullscreen" Translucent effect, no title bar, full screen
    ? Android: theme = "@ android: style/Theme. Panel"

    AndroidThe Platform defines three font sizes:

    "? Android: attr/textAppearanceLarge"
    "? Android: attr/textAppearanceMedium"
    "? Android: attr/textAppearanceSmall"

    The new version seems to have added a Huge font Huge

    AndroidFont color:

    Android: textColor = "? Android: attr/textColorPrimary"
    Android: textColor = "? Android: attr/textColorSecondary"
    Android: textColor = "? Android: attr/textColorTertiary"
    Android: textColor = "? Android: attr/textColorPrimaryInverse"
    Android: textColor = "? Android: attr/textColorSecondaryInverse"

    AndroidOfProgressBarStyle:

    Style = "? Android: attr/progressBarStyleHorizontal"
    Style = "? Android: attr/progressBarStyleLarge"
    Style = "? Android: attr/progressBarStyleSmall"
    Style = "? Android: attr/progressBarStyleSmallTitle"

    Delimiter

    Horizontal:

    Android: layout_width = "fill_parent"
    Android: layout_height = "1dip"
    Android: background = "? Android: attr/listDivider "/>


    Vertical:

    Android: layout_height = "fill_parent"
    Android: background = "? Android: attr/listDivider "/>

    CheckBoxStyle

    Style = "? Android: attr/starStyle"


    Similar to the title bar EffectTextView
    Style = "? Android: attr/listSeparatorTextViewStyle"

    Other useful styles
    Android: layout_height = "? Android: attr/listPreferredItemHeight"
    Android: paddingRight = "? Android: attr/scrollbarSize"
    Style = "? Android: attr/windowTitleBackgroundStyle"
    Style = "? Android: attr/windowTitleStyle"
    Android: layout_height = "? Android: attr/windowTitleSize"
    Android: background = "? Android: attr/windowBackground"

    ModifyActivityTitle bar style

    For example, add




    Next, modify the AndroidManifest. xml file, find the Activity to customize the title bar, and add the android: theme value, for example:

    Remove allActivityTitle Bar of the interface
    Modify AndroidManifest. xml

    Add android: theme = "@ android: style/Theme. NoTitleBar" to the application tag"



    Refer:

    Http://www.cnblogs.com/bluestorm/archive/2013/03/20/2971742.html

    Http://blog.sina.com.cn/s/blog_62ef2f14010105vi.html

    Http://www.krislq.com/2013/04/android_class_change_skin/

    Http://blog.csdn.net/wsscy2004/article/details/7562909


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.