Android主题与样式
来源:互联网 发布:表格引用数据并加前缀 编辑:程序博客网 时间:2024/05/01 23:04
一、样式
样式是属性的集合,例如定义属性fontColor、fontSize、layout_width、layout_height等,以独立的资源文件存放在XML文件中,并设置样式的名称。
Android Style类似网页设计中的级联样式CSS设计思路,可以让设计与内容分离,并且可以方便的继承、覆盖、重用。
1.未使用Style
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#00FF00"
- android:text="@string/hello" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
下面看看,如何使用Style来改进。
2、使用Style
首先,在res/values/下创建Style XML资源文件,这里创建的Style资源文件名命名为styles.xml,这个可以自己自定义。
styles.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="wrap_content">
- <item name="android:layout_width">wrap_content</item>
- <item name="android:layout_height">wrap_content</item>
- </style>
- </resources>
使用样式:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- style="@style/wrap_content"
- android:textColor="#00FF00"
- android:text="@string/hello" />
- <TextView
- style="@style/wrap_content"
- android:text="@string/hello" />
- <Button
- style="@style/wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
3、样式的继承
有两种方式来实现继承,一是通过style的parent属性,二是使用类似CSS中的命名规则来实现。
一、通过parent属性
修改styles.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="wrap_content">
- <item name="android:layout_width">wrap_content</item>
- <item name="android:layout_height">wrap_content</item>
- </style>
- <style name="inherit" parent="wrap_content">
- <item name="android:textColor">#00FF00</item>
- </style>
- </resources>
引用方式:style="@style/inherit"
二、通过命名规则实现
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="wrap_content">
- <item name="android:layout_width">wrap_content</item>
- <item name="android:layout_height">wrap_content</item>
- </style>
- <style name="wrap_content.inherit">
- <item name="android:textColor">#00FF00</item>
- </style>
- </resources>
引用方式:style="@style/wrap_content.inherit"
二、主题
针对应用中所有Activity或者针对某个Activity设置样式,可以通过编辑AndroidManifest.xml来完成。
1.设置应用中所有Activity活动的主题
- <application android:theme="@style/wrap_content">
2.设置某个指定的Activity主题
- <activity android:theme="@style/wrap_content">
另外,android提供了许多自带的主题样式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很简单
- <activity android:theme="@android:style/Theme.Dialog">
主题属性参数:http://android.toolib.net/reference/android/R.styleable.html#Theme
样式与主题参考:http://android.toolib.net/guide/topics/ui/themes.html
0 0
- Android样式与主题
- Android主题与样式
- Android主题与样式
- android样式与主题
- android样式与主题
- Android主题与样式
- Android主题与样式
- android主题与样式
- Android样式与主题
- Android主题与样式
- Android样式与主题
- Android 的样式与主题
- Android 中的样式与主题
- Android样式与主题浅析
- android中的样式与主题
- Android初试--Android中的样式与主题
- android的样式(style)与主题(theme)
- android的样式(style)与主题(theme)
- 160crackme-4
- 黑马程序员 JAVA基础--IO流(二)
- js 正则表达式语法
- eclipse中导入第三方jar包的源码
- 02-3. Pop Sequence (25)
- Android主题与样式
- STL源码剖析 - 第4章 序列式容器 - vector
- 基于管道的即时通讯(java nio)
- 【解决方案】#1045 - Access denied for user 'root'@'localhost' (using password: NO)
- java中的回调函数理解
- 用户输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- 开始写博客吧
- leetCode #189 Rotate Array
- (原创)详解KMP算法