Android 主题及自定义窗口
2015-10-26 09:55
411 查看
用主题自定义窗口属性
使用Android系统主题:
R.styles.xml中包含了系统的一些主题;
我们常见的系统主题有:
Theme.Light : 该主题的背景和用户元素使用一个相反的颜色主题. 为Android3.0以前的默认推荐基础主题;
<style name="Theme.Light">
<item name="isLightTheme">true</item>
<item name="windowBackground">@drawable/screen_background_selector_light</item>
<item name="windowClipToOutline">false</item>
<item name="colorBackground">@color/background_light</item>
<item name="colorForeground">@color/bright_foreground_light</item>
<item name="colorForegroundInverse">@color/bright_foreground_light_inverse</item>
<item name="colorPrimaryDark">@color/legacy_light_primary_dark</item>
<item name="colorPrimary">@color/legacy_light_primary</item>
<item name="colorControlActivated">@color/legacy_light_control_activated</item>
<item name="colorControlNormal">@color/legacy_light_control_normal</item>
<item name="colorControlHighlight">@color/legacy_light_button_pressed</item>
<item name="colorButtonNormal">@color/legacy_light_button_normal</item>
<item name="textColorPrimary">@color/primary_text_light</item>
<item name="textColorSecondary">@color/secondary_text_light</item>
<item name="textColorTertiary">@color/tertiary_text_light</item>
<item name="textColorPrimaryInverse">@color/primary_text_dark</item>
<item name="textColorSecondaryInverse">@color/secondary_text_dark</item>
<item name="textColorTertiaryInverse">@color/tertiary_text_dark</item>
<item name="textColorPrimaryActivated">@color/primary_text_light</item>
<item name="textColorSecondaryActivated">@color/secondary_text_light</item>
<item name="textColorPrimaryDisableOnly">@color/primary_text_light_disable_only</item>
<item name="textColorPrimaryInverseDisableOnly">@color/primary_text_dark_disable_only</item>
<item name="textColorPrimaryNoDisable">@color/primary_text_light_nodisable</item>
<item name="textColorSecondaryNoDisable">@color/secondary_text_light_nodisable</item>
<item name="textColorPrimaryInverseNoDisable">@color/primary_text_dark_nodisable</item>
<item name="textColorSecondaryInverseNoDisable">@color/secondary_text_dark_nodisable</item>
<item name="textColorHint">@color/hint_foreground_light</item>
<item name="textColorHintInverse">@color/hint_foreground_dark</item>
<item name="textColorHighlight">@color/highlighted_text_light</item>
<item name="textColorHighlightInverse">@color/highlighted_text_dark</item>
<item name="textColorLink">@color/link_text_light</item>
<item name="textColorLinkInverse">@color/link_text_dark</item>
<item name="editTextColor">@color/primary_text_light</item>
<item name="listChoiceBackgroundIndicator">@drawable/list_selector_background</item>
<item name="activatedBackgroundIndicator">@drawable/activated_background_light</item>
<item name="quickContactBadgeOverlay">@drawable/quickcontact_badge_overlay_light</item>
<item name="popupWindowStyle">@style/Widget.PopupWindow</item>
<item name="textCheckMark">@drawable/indicator_check_mark_light</item>
<item name="textCheckMarkInverse">@drawable/indicator_check_mark_dark</item>
<item name="gestureOverlayViewStyle">@style/Widget.GestureOverlayView.White</item>
<item name="expandableListViewStyle">@style/Widget.ExpandableListView.White</item>
<item name="listViewStyle">@style/Widget.ListView.White</item>
<item name="listDivider">@drawable/divider_horizontal_bright</item>
<item name="listSeparatorTextViewStyle">@style/Widget.TextView.ListSeparator.White</item>
<item name="progressBarStyle">@style/Widget.ProgressBar.Inverse</item>
<item name="progressBarStyleSmall">@style/Widget.ProgressBar.Small.Inverse</item>
<item name="progressBarStyleLarge">@style/Widget.ProgressBar.Large.Inverse</item>
<item name="progressBarStyleInverse">@style/Widget.ProgressBar</item>
<item name="progressBarStyleSmallInverse">@style/Widget.ProgressBar.Small</item>
<item name="progressBarStyleLargeInverse">@style/Widget.ProgressBar.Large</item>
<item name="actionModeCutDrawable">@drawable/ic_menu_cut_holo_light</item>
<item name="actionModeCopyDrawable">@drawable/ic_menu_copy_holo_light</item>
<item name="actionModePasteDrawable">@drawable/ic_menu_paste_holo_light</item>
<item name="actionModeSelectAllDrawable">@drawable/ic_menu_selectall_holo_light</item>
<item name="actionModeShareDrawable">@drawable/ic_menu_share_holo_light</item>
<item name="actionModeFindDrawable">@drawable/ic_menu_find_holo_light</item>
<item name="actionModeWebSearchDrawable">@drawable/ic_menu_search_holo_light</item>
<item name="actionModeBackground">@drawable/cab_background_top_holo_light</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_holo_light</item>
<item name="searchViewStyle">@style/Widget.Holo.Light.SearchView</item>
<item name="detailsElementBackground">@drawable/panel_bg_holo_light</item>
<item name="mediaRouteButtonStyle">@style/Widget.DeviceDefault.Light.MediaRouteButton</item>
<item name="findOnPageNextDrawable">@drawable/ic_find_next_holo_light</item>
<item name="findOnPagePreviousDrawable">@drawable/ic_find_previous_holo_light</item>
<!-- Floating toolbar styles -->
<item name="floatingToolbarCloseDrawable">@drawable/ic_ab_back_material_light</item>
<item name="floatingToolbarForegroundColor">@color/foreground_material_light</item>
<item name="floatingToolbarItemBackgroundBorderlessDrawable">@drawable/item_background_borderless_material_light</item>
<item name="floatingToolbarItemBackgroundDrawable">@drawable/item_background_material_light</item>
<item name="floatingToolbarOpenDrawable">@drawable/ic_menu_moreoverflow_material_light</item>
<item name="floatingToolbarPopupBackgroundDrawable">@drawable/floating_popup_background_light</item>
</style>
Theme.AppCompat.NoActionBar: 移除导航条标题栏
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
...
在manifest使用系统主题:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.akm.akmviewpager" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".MainActivity"
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>
</manifest>
自定义主题:
系统的主题毕竟不能满足各种奇葩的需求,需求我们自定义主题来解决这个问题;
1.创建主题
其实主题就是多种风格的样式,所以创建主题放在res/values/styles.xml; 有关主题的元素可以看SDK的R.attr中查看,上面系统主题中我们已经可以看到比较多的属性元素;
<style name="Akm" parent="android:Theme">
<!--action bar-->
<item name="android:windowActionBar">false</item>
<!-- title -->
<item name="android:windowNoTitle">false</item>
<!-- 窗口背景-->
<item name="android:windowBackground">#ff00ff</item>
<!-- true 全屏显示-->
<item name="android:windowFullscreen">true</item>
</style>
主题类似Java类的关系, 是可以继承的.
代码设置:
Activity.requestWindowFeature()方法修改窗口特性,该方法接收 int类型的 featureId
值得注意是该方法需要在setContentView()之前设置,在此后的设置不起效;
我们可以看看系统的window.java来简单了解下可以设置的元素
/** Flag for the "options panel" feature. This is enabled by default. */
public static final int FEATURE_OPTIONS_PANEL = 0;
/** Flag for the "no title" feature, turning off the title at the top
* of the screen. */
public static final int FEATURE_NO_TITLE = 1;//无标题
/** Flag for the progress indicator feature */
public static final int FEATURE_PROGRESS = 2;//进度条
/** Flag for having an icon on the left side of the title bar */
public static final int FEATURE_LEFT_ICON = 3;//左图标
/** Flag for having an icon on the right side of the title bar */
public static final int FEATURE_RIGHT_ICON = 4;//右图标
/** Flag for indeterminate progress */
public static final int FEATURE_INDETERMINATE_PROGRESS = 5;
/** Flag for the context menu. This is enabled by default. */
public static final int FEATURE_CONTEXT_MENU = 6;//菜单
/** Flag for custom title. You cannot combine this feature with other title features. */
public static final int FEATURE_CUSTOM_TITLE = 7;//自定义标头
/**
* Flag for enabling the Action Bar.
* This is enabled by default for some devices. The Action Bar
* replaces the title bar and provides an alternate location
* for an on-screen menu button on some devices.
*/
public static final int FEATURE_ACTION_BAR = 8;//导航栏
......
很多应用的夜间模式,就是设置主题样式来实现的.只不过需要代码控制切换
<pre style="font-family: Menlo; font-size: 12pt; background-color: rgb(255, 255, 255);">
使用Android系统主题:
R.styles.xml中包含了系统的一些主题;
我们常见的系统主题有:
Theme.Light : 该主题的背景和用户元素使用一个相反的颜色主题. 为Android3.0以前的默认推荐基础主题;
<style name="Theme.Light">
<item name="isLightTheme">true</item>
<item name="windowBackground">@drawable/screen_background_selector_light</item>
<item name="windowClipToOutline">false</item>
<item name="colorBackground">@color/background_light</item>
<item name="colorForeground">@color/bright_foreground_light</item>
<item name="colorForegroundInverse">@color/bright_foreground_light_inverse</item>
<item name="colorPrimaryDark">@color/legacy_light_primary_dark</item>
<item name="colorPrimary">@color/legacy_light_primary</item>
<item name="colorControlActivated">@color/legacy_light_control_activated</item>
<item name="colorControlNormal">@color/legacy_light_control_normal</item>
<item name="colorControlHighlight">@color/legacy_light_button_pressed</item>
<item name="colorButtonNormal">@color/legacy_light_button_normal</item>
<item name="textColorPrimary">@color/primary_text_light</item>
<item name="textColorSecondary">@color/secondary_text_light</item>
<item name="textColorTertiary">@color/tertiary_text_light</item>
<item name="textColorPrimaryInverse">@color/primary_text_dark</item>
<item name="textColorSecondaryInverse">@color/secondary_text_dark</item>
<item name="textColorTertiaryInverse">@color/tertiary_text_dark</item>
<item name="textColorPrimaryActivated">@color/primary_text_light</item>
<item name="textColorSecondaryActivated">@color/secondary_text_light</item>
<item name="textColorPrimaryDisableOnly">@color/primary_text_light_disable_only</item>
<item name="textColorPrimaryInverseDisableOnly">@color/primary_text_dark_disable_only</item>
<item name="textColorPrimaryNoDisable">@color/primary_text_light_nodisable</item>
<item name="textColorSecondaryNoDisable">@color/secondary_text_light_nodisable</item>
<item name="textColorPrimaryInverseNoDisable">@color/primary_text_dark_nodisable</item>
<item name="textColorSecondaryInverseNoDisable">@color/secondary_text_dark_nodisable</item>
<item name="textColorHint">@color/hint_foreground_light</item>
<item name="textColorHintInverse">@color/hint_foreground_dark</item>
<item name="textColorHighlight">@color/highlighted_text_light</item>
<item name="textColorHighlightInverse">@color/highlighted_text_dark</item>
<item name="textColorLink">@color/link_text_light</item>
<item name="textColorLinkInverse">@color/link_text_dark</item>
<item name="editTextColor">@color/primary_text_light</item>
<item name="listChoiceBackgroundIndicator">@drawable/list_selector_background</item>
<item name="activatedBackgroundIndicator">@drawable/activated_background_light</item>
<item name="quickContactBadgeOverlay">@drawable/quickcontact_badge_overlay_light</item>
<item name="popupWindowStyle">@style/Widget.PopupWindow</item>
<item name="textCheckMark">@drawable/indicator_check_mark_light</item>
<item name="textCheckMarkInverse">@drawable/indicator_check_mark_dark</item>
<item name="gestureOverlayViewStyle">@style/Widget.GestureOverlayView.White</item>
<item name="expandableListViewStyle">@style/Widget.ExpandableListView.White</item>
<item name="listViewStyle">@style/Widget.ListView.White</item>
<item name="listDivider">@drawable/divider_horizontal_bright</item>
<item name="listSeparatorTextViewStyle">@style/Widget.TextView.ListSeparator.White</item>
<item name="progressBarStyle">@style/Widget.ProgressBar.Inverse</item>
<item name="progressBarStyleSmall">@style/Widget.ProgressBar.Small.Inverse</item>
<item name="progressBarStyleLarge">@style/Widget.ProgressBar.Large.Inverse</item>
<item name="progressBarStyleInverse">@style/Widget.ProgressBar</item>
<item name="progressBarStyleSmallInverse">@style/Widget.ProgressBar.Small</item>
<item name="progressBarStyleLargeInverse">@style/Widget.ProgressBar.Large</item>
<item name="actionModeCutDrawable">@drawable/ic_menu_cut_holo_light</item>
<item name="actionModeCopyDrawable">@drawable/ic_menu_copy_holo_light</item>
<item name="actionModePasteDrawable">@drawable/ic_menu_paste_holo_light</item>
<item name="actionModeSelectAllDrawable">@drawable/ic_menu_selectall_holo_light</item>
<item name="actionModeShareDrawable">@drawable/ic_menu_share_holo_light</item>
<item name="actionModeFindDrawable">@drawable/ic_menu_find_holo_light</item>
<item name="actionModeWebSearchDrawable">@drawable/ic_menu_search_holo_light</item>
<item name="actionModeBackground">@drawable/cab_background_top_holo_light</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_holo_light</item>
<item name="searchViewStyle">@style/Widget.Holo.Light.SearchView</item>
<item name="detailsElementBackground">@drawable/panel_bg_holo_light</item>
<item name="mediaRouteButtonStyle">@style/Widget.DeviceDefault.Light.MediaRouteButton</item>
<item name="findOnPageNextDrawable">@drawable/ic_find_next_holo_light</item>
<item name="findOnPagePreviousDrawable">@drawable/ic_find_previous_holo_light</item>
<!-- Floating toolbar styles -->
<item name="floatingToolbarCloseDrawable">@drawable/ic_ab_back_material_light</item>
<item name="floatingToolbarForegroundColor">@color/foreground_material_light</item>
<item name="floatingToolbarItemBackgroundBorderlessDrawable">@drawable/item_background_borderless_material_light</item>
<item name="floatingToolbarItemBackgroundDrawable">@drawable/item_background_material_light</item>
<item name="floatingToolbarOpenDrawable">@drawable/ic_menu_moreoverflow_material_light</item>
<item name="floatingToolbarPopupBackgroundDrawable">@drawable/floating_popup_background_light</item>
</style>
Theme.AppCompat.NoActionBar: 移除导航条标题栏
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
...
在manifest使用系统主题:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.akm.akmviewpager" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".MainActivity"
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>
</manifest>
自定义主题:
系统的主题毕竟不能满足各种奇葩的需求,需求我们自定义主题来解决这个问题;
1.创建主题
其实主题就是多种风格的样式,所以创建主题放在res/values/styles.xml; 有关主题的元素可以看SDK的R.attr中查看,上面系统主题中我们已经可以看到比较多的属性元素;
<style name="Akm" parent="android:Theme">
<!--action bar-->
<item name="android:windowActionBar">false</item>
<!-- title -->
<item name="android:windowNoTitle">false</item>
<!-- 窗口背景-->
<item name="android:windowBackground">#ff00ff</item>
<!-- true 全屏显示-->
<item name="android:windowFullscreen">true</item>
</style>
主题类似Java类的关系, 是可以继承的.
代码设置:
Activity.requestWindowFeature()方法修改窗口特性,该方法接收 int类型的 featureId
值得注意是该方法需要在setContentView()之前设置,在此后的设置不起效;
我们可以看看系统的window.java来简单了解下可以设置的元素
/** Flag for the "options panel" feature. This is enabled by default. */
public static final int FEATURE_OPTIONS_PANEL = 0;
/** Flag for the "no title" feature, turning off the title at the top
* of the screen. */
public static final int FEATURE_NO_TITLE = 1;//无标题
/** Flag for the progress indicator feature */
public static final int FEATURE_PROGRESS = 2;//进度条
/** Flag for having an icon on the left side of the title bar */
public static final int FEATURE_LEFT_ICON = 3;//左图标
/** Flag for having an icon on the right side of the title bar */
public static final int FEATURE_RIGHT_ICON = 4;//右图标
/** Flag for indeterminate progress */
public static final int FEATURE_INDETERMINATE_PROGRESS = 5;
/** Flag for the context menu. This is enabled by default. */
public static final int FEATURE_CONTEXT_MENU = 6;//菜单
/** Flag for custom title. You cannot combine this feature with other title features. */
public static final int FEATURE_CUSTOM_TITLE = 7;//自定义标头
/**
* Flag for enabling the Action Bar.
* This is enabled by default for some devices. The Action Bar
* replaces the title bar and provides an alternate location
* for an on-screen menu button on some devices.
*/
public static final int FEATURE_ACTION_BAR = 8;//导航栏
......
很多应用的夜间模式,就是设置主题样式来实现的.只不过需要代码控制切换
<pre style="font-family: Menlo; font-size: 12pt; background-color: rgb(255, 255, 255);">
相关文章推荐
- Android中内容观察者的使用---- ContentObserver类详解
- android横竖屏切换
- android 用 XML 自定义边框(只上下边框有色)
- Android开发技巧-Fragment的懒加载
- 安卓开发学习之005 LinearLayout之Weight/measureWithLargestChild详解
- android android.view.InflateException: Binary XML file line #14: Error inflating class<unknown>
- android在Service,BroadCast onReceiver()中弹出Dialog对话框
- Android_10_Java调用C代码(纯手工制作)
- Android创建项目时候为什么是RelativeLayout
- Android studio 中的Theme Editor神器
- Android 4.0新组件:GridLayout详细说明
- Android版本和API Level对应关系
- Android的Socket通信编程实例
- Android Studio 引入第三方库
- Android修改TitleBar标题栏详解
- Android基础控件之Button的基本使用
- Android逆向之旅---Android应用的汉化功能(修改SO中的字符串内容)
- 倍数提高工作效率的 Android Studio 奇技
- Android USB Host 通信程序
- FFmpeg的Android平台移植—编译篇