AndroidStyle分为两种:Theme是针对窗体级别的,改变窗体的样式;Style是针对窗体元素级别的,改变指定控件或者Layout样式。

Android系统的主题和样式位于frameworks\base\core\res\res\values,其中包含很多系统预先定义好的style,可以直接引用或修改。

自定义样式

Android中除了系统内置的样式外,我们还可以自定义样式,步骤如下:

在res/values目录新建style.xml文件,并增加<resources>根节点。
对每个主题或风格增加唯一名称,同时也可以继承某个风格。
在 <style> 元素内部,申明一个或多个 <item>, 并得以名称属性,并在元素内部定义该风格的值。
最后可以通过@style应用在其他xml中。

实例如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SpecialText" parent="@style/Text">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#008</item>
    </style>
</resources>  

抽取和继承

样式除了可以抽取公共的样式使用之外,还可以通过继承的方式来实现样式的父子关系。

样式的抽取

为了提高布局文件复用性,我们可以将相同控件的属性进行抽取,在res/string.xml中添加style节点,每个属性对应一个item子节点

<style name="a">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#FF0000</item>
    <item name="android:textSize">20sp</item>
</style>

样式的继承

1、当一种样式具有另一种样式相同的属性,并且还有自特有的属性,我们就可以使用继承样式。

<style name="a">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#FF0000</item>
    <item name="android:textSize">20sp</item>
</style>

<style name="b" parent="a"/>

继承的第二种方式

<style name="b.c">
    <item name="android:layout_gravity">right</item>
    <item name="android:textColor">#00FF00</item>
</style>

修改系统样式

1、查找D:\Android\sdk\platforms\android-18\data\res\values目录下的styles.xml文件

2、文件中搜索需要用到的控件,进行观察,这里以ProgressBar系统样式为例。

3、发现其实ProgressBar引用了一个图片资源,那么使用工具搜索到Progress_medium_white,发现其实是一个xml文件,也就是一个旋转动画。

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:andro
    android:drawable="@drawable/spinner_white_48"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    android:frameDuration="100" />

4、旋转动画中又引用了spinner_white_48,发现它其实是一张图片,所以我们可以修改ProgressBar的样式图片。

5、拷贝第3步的内容,也就是布局动画文件,然后在drawable目录下创建一个xml文件,将拷贝的内容复制进去,再修改drawable属性下对应的图片。

6、在布局文件的ProgressBar中使用indeterminateDrawable引用该xml资源文件。

主题

自定义主题

主题在<style>元素中申明,引用方式和Style一样的,不同的是通过在清单文件定义的<application>和<activity>元素将主题添加到某个程序,但是主题不能应用在view中。

<resources>
    <style name="CustomTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="windowFrame">@drawable/screen_frame</item>
        <item name="windowBackground">@drawable/screen_background_white</item>
        <item name="panelForegroundColor">#FF000000</item>
        <item name="panelBackgroundColor">#FFFFFFFF</item>
        <item name="panelTextColor">?panelForegroundColor</item>
        <item name="panelTextSize">14</item>
        <item name="menuItemTextColor">?panelTextColor</item>
        <item name="menuItemTextSize">?panelTextSize</item>
    </style>
</resources>  

@符号:表示我们应用的资源是前面定义过的或在前一个项目中又或者在Android框架中,?符号:表示我们引用的资源的值在当前的主题当中定义过的。

主题设置

Android设置主题有两种方式:静态设置 和 动态设置。

静态设置主题

1、如果在所有的activity中使用主题,则将主题设置在<application>标签中。

<application android:theme="@style/CustomTheme">  

2、如果只让某个activity拥有主题,则将主题设置在<activity>>标签中。

<activity android:theme="@android:style/Theme.Dialog">  

3、如果针对某个主题进行轻微改变,只需要在style中将改主题设置为父主题即可。

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog"> 

动态设置主题

当我们想在代码setTheme()对主题进行动态设置的话,则必须在初始化任何view之前进行设置,比如在setContentView(View)或inflate(int,ViewGroup)方法之前。

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

如果在代码中动态加载主界面主题,那么注意主题当中不能包括任何系统启动这个activity锁使用的动画,这些动画将在程序前显示。

Activity默认动画

a) 首先在res文件夹下建立anim文件夹,然后在里面建立fase_in.xml和fade_out.xml两个动画资源

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:andro
    android:duration="300"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0" />  

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:andro
    android:duration="300"
    android:fromAlpha="1.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="0.0" />

b) 然后在values文件夹下的style.xml中的resources标签内写:

<style name="Anim_fade" parent="android:Theme.NoTitleBar">
    <item name="android:windowAnimationStyle">@style/fade</item>
</style>
<style name="fade" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/fade_in</item>
    <item name="android:activityOpenExitAnimation">@anim/fade_out</item>
    <item name="android:activityCloseEnterAnimation">@anim/fade_in</item>
    <item name="android:activityCloseExitAnimation">@anim/fade_out</item>
</style>  

c) 最后一步在AndroidManifest.xml中的Activity的声明上加入android:theme="@style/Anim_fade"

<activity android:name=".MainActivity"
    android:theme="@style/anim_fade">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

注意:在高版本的Android下可能会没有效果。

Material Design

Material Design 是将经典的设计原则和科技、创新相结合而创造的设计语言。

基本介绍

作为我们开发者,最关心的还是如何在项目中使用Material Design风格:

设置应用的 targetSdkVersion 和 targetSdkVersion 为21
在values目录下的style资源文件中创建一个style,让其继承自 android:Theme.Material
在AndroidManifest中指定应用的主题或者Activity的主题为我们设定的样式

谷歌官方我们提供了三种配色风格的Material Design样式:

黑色主题 Theme.Material
明亮主题 Theme.Material.Light
明亮主题黑色ActionBar Theme.Material.Light.DarkActionBar

我们也可以继承系统提供的Material Design样式,进行配色修改:
03、Android--资源管理

其中,使用到的属性如下所示:

android:colorPrimaryDark 应用的主要暗色调,statusBarColor默认使用该颜色
android:statusBarColor 状态栏颜色,默认使用colorPrimaryDark
android:colorPrimary 应用的主要色调,actionBar默认使用该颜色
android:windowBackground 窗口背景颜色
android:navigationBarColor 底部栏颜色
android:colorForeground 应用的前景色,ListView的分割线,switch滑动区默认使用该颜色
android:colorBackground 应用的背景色,popMenu的背景默认使用该颜色
android:colorAccent 一般控件的选种效果默认采用该颜色
android:colorControlNormal 控件的默认色调 
android:colorControlHighlight 控件按压时的色调
android:colorControlActivated 控件选中时的颜色,默认使用colorAccent
android:colorButtonNormal 默认按钮的背景颜色
android:textColor Button,textView的文字颜色
android:textColorPrimaryDisableOnly RadioButton checkbox等控件的文字
android:textColorPrimary 应用的主要文字颜色,actionBar的标题文字默认使用该颜色

兼容性方案

Material Design主题只有在API级别为21以上才可使用,所以我们要兼容低版本的只需要做如下步骤即可:

在build.grade中添加Material Design的支持:

compile 'com.android.support:appcompat-v7:version'
compile 'com.android.support:design:version'

从5.0 API-21开始支持Material Desigh主题,如果要直接使用,那么需要设置应用的minSdkVersion = 21:

android:Theme.Material
android:Theme.Material.Light
android:Theme.Material.Light.DarkActionBar

如果需要兼容低版本,在Support包提供了兼容主题:

Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar

在color.mxl中定义需要的颜色

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

在styles.xml中定义主题

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

在res目录下,创建一个values-v21目录,再创建一个styles.xml:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">?colorPrimaryDark</item>
</style>

在AndroidManifest.xml中设置主题即可。

资源目录

资源国际化

字符串国际化

字符串的国际化只需要在res文件夹下新建对应语言的values文件夹即可。

国家 文件名
简体中文(大陆) values-zh-rCN
繁体中文(台湾) values-zh-rTW
繁体中文(香港) values-zh-rHK
英语(美国) values-en-rUS
英语(应该) values-en-rGB
德文(德国) values-de-rDE
法文(法国) values-fr-rFR
日文 values-ja-rJP
韩文 values-ko-rKR

图片国际化

在res下新建drawable-zh文件夹,存放中文环境下的图片,其他语言一样。

在eclipse新建Android项目时,会在res目录下自动创建多个默认语言环境的文件夹

drawable-mdpi
drawable-ldpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi

分别用于存放高、中、低等分辨率图片。Android系统会根据手机的分辨率自动从不同的对应的某个文件夹下去加载图片。同时,它们也是可以进行国际化的,命名规则如下:

drawable-zh-hdpi
drawable-en-ldpi
drawable-en-rUS-mdpi

注意:xml中使用资源表达式: @drawable/icon;代码使用资源表达式: R.drawable.icon。

自定义资源

String定义

1、定义string类型

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello" >Hello World, MainActivity!
    </string>
    <string name="app_name" >TestExample01
    </string>
</resources>  

使用方式:

String appName = (String) this.getResources().getText(R.string.app_name);
或
String appName = (String) this.getResources().getString(R.string.app_name);  

2、定义string数组(arrays.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="sports">
    <item>足球</item>
    <item>篮球</item>
    <item>太极</item>
    <item>冰球</item>
  </string-array>
</resources>    

使用方式:

getResources().getStringArray(R.array.sports);

3、定义颜色(colors.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FFFFFF</color>
</resources> 

使用方式:

getResources().getDrawable(R.string.black);
或
getResources().getColor(R.string.black);   

4、定义尺寸(dimens.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <dimen name="height">80dip</dimen>
</resources>

使用方式:

getResource().getDimension(R.string.height);  

5、定义样式(style.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="sharpText">
       <item name="android:textSize">18sp</item>
       <item name="android:textColor">#000000</item>
    </style>
</resources>  

String占位符

在Android中,如果需要动态的修改value/string.xml中的值,则可以使用占位符string.xml中节点是支持占位符的,如下所示:

<string name="data">整数型:%1$d,浮点型:%2$.2f,字符串:%3$s</string>  

获取字符串并进行格式化,需要注意的是浮点型数据中的.2表示保留多少位小数。

String data = getResources().getString(R.string.data);
data = String.format(data, 100,10.3,"2016-02-14");  

相关文章:

  • 2021-08-30
  • 2021-11-18
  • 2021-06-27
猜你喜欢
  • 2021-06-19
  • 2021-04-15
  • 2021-05-14
  • 2022-01-06
  • 2022-02-14
  • 2021-09-21
  • 2021-08-02
相关资源
相似解决方案