打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android Theme 常见主题风格详解

本文为自己多年来在Android实战开发过程中总结归纳的一些常见问题,现在分享出来希望对初学者有所帮助。 

本文出自门心叼龙的博客,转载请注明出处: https://blog.csdn.net/geduo_83/article/details/86560896     

目录


1. 什么是Style,什么是Theme?

  • 1.1 联系        

Style 和 theme:是一个包含一种 或者 多种格式化 属性 的集合  ,并且 style和theme都是资源,存放在res/values 文件夹下 

  • 1.2 区别:

style:View级别的,只能在某个Activity的布局文件中使用
Theme:应用级别的,你必须在AndroidManifest.xml中 的<application>或者<activity>中使用

2.  在定义Theme的时候@符号和?符号有何区别?

@符号 表明 我们引用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明 我们引用的资源的值在 当前的 主题当中定义过    

3. 怎么通过代码给一个Activity设置主题?

  1. protected void onCreate(Bundle savedInstanceState) {
  2.      super.onCreate(savedInstanceState); 
  3.      setTheme(android.R.style.Theme_Light); 
  4.      setContentView(R.layout.linear_layout_3); 
  5. }

4. AppTheme主题颜色colorPrimary,colorPrimaryDark,colorAccent都是什么的颜色?

  • 4.1 colorPrimary

App Bar 的背景色,即 ActionBar,通常也是一个 App 的主题色调。不过 ActionBar 已经退出历史舞台,由 Toolbar 代替使用,但是 Toolbar 需要在 layout 文件中单独使用 background 属性设置背景色,如:

  1. <android.support.v7.widget.Toolbar
  2.   android:layout_width="match_parent"
  3.   android:layout_height="wrap_content"
  4.   android:background="?attr/colorPrimary" />
  • 4.2 colorPrimaryDark

status bar(状态栏)背景色。仅作用于 Lollipop 及更高版本。

  • 4.3 colorAccent

许多控件在选中状态或获取焦点状态下使用这个颜色,常见有:

  • CheckBox:checked 状态
  • RadioButton:checked 状态
  • SwitchCompat:checked 状态
  • EditText:获取焦点时的 underline 和 cursor 颜色
  • TextInputLayout:悬浮 label 字体颜色

        等等

  • 4.4 android:navigationBarColor

navigation bar 背景色。仅作用于 Lollipop 及更高版本。

  • 4.5 colorControlNormal

某些 Views “normal” 状态下的颜色,常见如:unselected CheckBox 和 RadioButton,失去焦点时的 EditText,Toolbar 溢出按钮颜色,等等。

  • 4.6 colorControlActivated

某种程度上,是 colorAccent 的替代者,比如对于 CheckBox 和 RadioButton 的 checked 状态,colorControlActivated 属性会覆盖 colorAccent 属性的对应颜色。

  • 4.7 colorControlHighlight

所有可点击 Views 触摸状态下的 Ripple(涟漪)效果。仅作用于 Lollipop 及更高版本。

  • 4.8 colorButtonNormal

Button normal 状态下的背景色。注意,这种设置与 Button 的 android:background 属性改变背景色不同的是,前者在 Lollipop 及更高版本上会让 Button 依旧保持阴影和 Ripple 触摸效果。

  • 4.9 android:windowBackground

窗口背景色,诸如此类的还有:android:background,android:colorBackground 等。

  • 4.10 android:textColorPrimary

EditText 的 text color,等等文本颜色。

  • 4.11 navigationIcon

返回按钮的图片




5.常见的主题风格都有哪些?

android:theme="@android:style/Theme.Dialog"   将一个Activity显示为能话框模式  
android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏  
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏  
android:theme="Theme.Light"  背景为白色  
android:theme="Theme.Light.NoTitleBar"  白色背景并无标题栏   
android:theme="Theme.Light.NoTitleBar.Fullscreen"  白色背景,无标题栏,全屏  
android:theme="Theme.Black"  背景黑色  
android:theme="Theme.Black.NoTitleBar"  黑色背景并无标题栏  
android:theme="Theme.Black.NoTitleBar.Fullscreen"    黑色背景,无标题栏,全屏  
android:theme="Theme.Wallpaper"  用系统桌面为应用程序背景  
android:theme="Theme.Wallpaper.NoTitleBar"  用系统桌面为应用程序背景,且无标题栏  
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  用系统桌面为应用程序背景,无标题栏,全屏  
android:theme="Translucent"  半透明  
android:theme="Theme.Translucent.NoTitleBar" 半透明、无标题栏  
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏  
android:theme="Theme.Panel"  
android:theme="Theme.Light.Panel"  

6.ThemeOverlay使用特点?

当在某个Activity有些特殊要求的时候就可以用ThemeOverlay继承全局的样式,来修改自己的个性化样式,注意了该样式的引用只能设置在布局文件上,不能在清单文件里面进行设置
定义:

  1. <style name="AppTheme.Secondary" parent="ThemeOverlay.AppCompat">
  2.     <item name="colorAccent">@color/colorPrimary</item>
  3. </style>

调用:

  1. <FrameLayout
  2.     android:background=”@color/dark_background”
  3.     android:theme="@style/ThemeOverlay.AppCompat.Dark”>
  4.   <TextView />
  5. </FrameLayout>

单独给toolbar设置样式

  1. <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="?android:attr/actionBarSize"
  4.     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  5.     android:background="?attr/colorPrimary">
  6. </android.support.v7.widget.Toolbar>

7. 自定义样式属性

  • 7.1 首先在attrs.xml 定义属性名称
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <declare-styleable name="AppTheme.NoActionBar">
  4.         <attr name="baseTitleTextColor" format="reference|color" />
  5.         <attr name="titleDividerColor" format="reference|color" />
  6.         <attr name="titleDividerLine" format="dimension" /> 
  7.     </declare-styleable>
  8. </resources>
  • 7.2 在style.xml中使用自定义的属性
  1. <style name="AppTheme.NoActionBar">
  2.         <item name="windowActionBar">false</item>
  3.         <item name="windowNoTitle">true</item>
  4.         <item name="baseTitleTextColor">#2a2a2a</item>
  5.         <item name="titleDividerLine">1dp</item>
  6.         <item name="titleDividerColor">@android:color/transparent</item>
  7. </style>
  • 7.3 在布局文件中引用样式
  1.  <View
  2.             android:id="@+id/view_divider"
  3.             android:layout_width="match_parent"
  4.             android:layout_height="?attr/titleDividerLine"
  5.             android:background="?attr/titleDividerColor"/>

8. 自定义一个tootbar的样式?

  • 8.1 定义一个NoActionBar的样式
  1. <style name="TestAppTheme" parent="Theme.AppCompat.Light">
  2.             <item name="windowActionBar">false</item>
  3.         <item name="windowNoTitle">true</item>
  4.         <item name="colorPrimary">#6a1b9a</item>
  5.         <item name="colorPrimaryDark">#ec407a</item>
  6.         <item name="colorAccent">#f44336</item>
  7.     </style>
  • 8.2 布局中引人Toolbar
  1. <android.support.v7.widget.Toolbar   
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3.               android:orientation="vertical"
  4.               android:layout_width="match_parent"
  5.               android:background="#4e342e"
  6.               android:layout_height="wrap_content"
  7.               android:minHeight="?attr/actionBarSize">
  8. </android.support.v7.widget.Toolbar  >
  • 8.3 在Activity中设置Toobar为ActionBar
  1. public class TestAppComActivity extends AppCompatActivity{
  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         setContentView(R.layout.activity_testappcompat);
  6.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  7.         setSupportActionBar(toolbar);
  8.     }
  9. }

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android 使用Theme实现动态切换主题详细教程
深入辨析Android四大组件(九)
android4.0自定义标题报错 -----断点记录
Android夜间模式实现
colorAccent,colorPrimary,colorPrimaryDark
Android AppTheme 系統預設樣式android:Theme語法整理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服