Android 样式
android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。
在CSS中是这样定义的:
<style>.wu{COLOR:#0000CC;font-size:18px;}</style>
可以像这样使用上面的css样式:<div >wuyudong‘blog</div>
在Android中可以这样定义样式:
在res/values/styles.
<??><resources><style name=“wu”> <!-- 为样式定义一个全局唯一的名字--><item name=“android:textSize”>18px</item> <!-- name属性的值为使用了该样式的View控件的属性 --><item name="android:textColor">#0000CC</item></style></resources>
在layout文件中可以像下面这样使用上面的android样式:
<??><LinearLayout ="http://schemas.android.com/apk/res/android" ....><TextView style="@style/wu"..... /></LinearLayout>
下面来实践一下
在style.
<resources ="http://schemas.android.com/apk/res/android"> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.--> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.--> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <style name="text_content_style" parent="AppBaseTheme"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#66ff00</item> <item name="android:textSize">20sp</item> </style></resources>
布局代码如下:
<LinearLayout ="http://schemas.android.com/apk/res/android" ="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView style="@style/text_content_style" android:text="我是一个文本" /> <TextView style="@style/text_content_style" android:text="我是一个文本" /> <TextView style="@style/text_content_style" android:text="我是一个文本" /> <TextView style="@style/text_content_style" android:text="我是一个文本" /> <TextView style="@style/text_content_style" android:text="我是一个文本" /></LinearLayout>
运行项目后
images/loading.gif' data-original="http://images2015.cnblogs.com/blog/617081/201609/617081-20160912203217398-254088565.png" />
Android 主题
android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:
<??><resources><style name=“wuTheme"><item name=“android:windowNoTitle”>true</item> <!–- 没标题 --><item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!–- 全屏显示 --></style></resources>
上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.
<application android:icon="@drawable/icon" android:label="@string/app_name"android:theme="@style/wuTheme">......</application>
除了可以在AndroidManifest.
setTheme(R.style.itcastTheme);
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.
另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,还有透明主题:@android:style/Theme.Translucent 。如果需要查阅这些主题,可以在文档的referenceandroid-->R.style 中查看。
原标题:Android 样式和主题(style theme)
关键词:Android