android的风格

android的风格是一个描述android控件的(根控件是view)属性的集合,这些属性包括控件大小、背景颜色、甚至动态效果等视觉属性。androi风格使用xml文件进行描述然后在布局文件中引用,风格文件的xml名字是可以随意命名的(当然要以.xml为扩展名),放到android工程的res/values目录下面,android编译器在生成.apk文件时(android可执行文件)做为资源文件进行解析编译。

先看一个例子:

java代码 style.java

package tt.t;
import java.net.URL;

import android.app.Activity;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class TtActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
}

布局layout的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:baselineAligned="true">
<TextView
style="@style/CodeFont_blue"
android:text="@string/hello" />
</LinearLayout>

风格文件mystyle,这个文件在/res/values工程目录下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont_green" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000FF</item>
<item name="android:typeface">monospace</item>
</style>
</resources>

布局文件中引用的风格style="@style/CodeFont_blue"就是mystyle.xml中的风格


<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000FF</item>
<item name="android:typeface">monospace</item>
</style>
<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000FF</item>
<item name="android:typeface">monospace</item>
</style>
<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000FF</item>
<item name="android:typeface">monospace</item>
</style>
<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#0000FF</item>
<item name="android:typeface">monospace</item>
</style>

在mystyle.xml中描述了两个风格,风格文件xml的跟节点必须是resources节点(<resources>开始</resources>结尾),resources子节点是style节点代表一个风格,每个style的子节点是item,每个item可以描述一个风格的属性。

android风格继承

在android中系统提供了很多已经定义好的风格,我们可以直接继承过来可以修改在上面修改和添加风格元素,继承方法使用parent 关键字如上面的

<style name="CodeFont_blue" parent="@android:style/TextAppearance.Medium">

就是继承了android本身的@android:style/TextAppearance.Medium style。同样的方法可以继承我们自己的风格:

<style name="CodeFont_blue" parent="@android:style/CodeFont_green">

继承自己的风格可以简写:

<style name="CodeFont_green.change">
<item name="android:textColor">#FF0000</item>
</style>

甚至还可以

<style name="<stylename="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>.Big"
>
<item name="android:textSize">30sp</item>
</style>

androi的主题也可以继承,如:

<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme"parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>

风格继承类似面向对象编程语言如C++和java中类的继承,继承了一个父类的子类可以使用父类的方法,同时自己也可以添加或者修改父类的方法。风格继承类似子风格可以使用父类风格的属性,同时可以添加或者修改父类风格属性。我们知道了如何设置android控件风格那每个android控件都有哪些属性?android的控件属性可以在android的开发文档中查找每个控件的XML Attributes,上面列出了各个控件的风格属性。

andriod通过设定style和theme还有可以实现一些动画效果,这是网上的一篇文件:

http://www.eoeandroid.com/thread-653-1-1.html(谢谢这位大侠)

详细请参考android开发文档关于主题和风格的文章

http://developer.android.com/guide/topics/ui/themes.html

更多相关文章

  1. android操作sdcard中的多媒体文件(一)——音乐列表的制作
  2. android 操作sdcard中的多媒体文件(一)——音乐列表的制作
  3. Android webview加载html页面根据点击确定选中的控件
  4. Android Zip文件解压缩代码
  5. Android中多媒体文件、文档以及各类文件的获取
  6. Android 超简单的录制屏幕视频及生成GIF文件的方法

随机推荐

  1. 【新提醒】N820 N821 android 4.2 V1.1版
  2. 【Android开源项目分析】android轻量级开
  3. Gsensor的整个系统架构
  4. Android里的观察者模式应用
  5. Android,iPad minis点燃了平板市场
  6. Android sqlite 数据库操作
  7. Android(安卓)Resource的使用和介绍
  8. Android 7.0新特性
  9. Android对话框Dialog,PopupWindow,Toast的
  10. Android 拨打电话功能