UI基础style样式&Theme
来源:互联网 发布:java 程序员就业班 编辑:程序博客网 时间:2024/06/05 06:37
利用style可以为layout中任何对象以xml方式定义外观,例如给文设置textSize,textColor等,
新建一个xml文件,任意命名例如style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DavidStyleText1">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#EC9237</item>
</style>
<style name="DavidStyleText2">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FF7F7C</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
</style>
</resources>
每个style通过name属性区别或调用,例如在main.xml中为两个textview分别添加这两个样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 套用樣式1的TextView -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view1"
/>
<!-- 套用樣式2的TextView -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view2"
/>
</LinearLayout>
另外<style>元素中有一个parent属性。这个属性可以让当前样式继承一个父样式,并且具有父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="subitcast" parent="@style/parentstyle">
<item name="android:textColor">#FF0000</item>
</style>
</resources>
关于theme,他的定义形式上与style一样,两者的区别在与应用地方不同,theme针对整个应用或Activity
主题对整个应用或某个Activity进行全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题和样式属性发生冲突时,样式的优先级高于主题。
另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,还有透明主题:@android:style/Theme.Translucent 。如果需要查阅这些主题,可以在文档的referenceandroid-->R.style 中查看。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“itcastTheme">
<item name=“android:windowNoTitle”>true</item> <!– 没标题
<item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全屏显示
</style>
</resources>
上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@style/myTheme">
......
</application>
除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.myTheme);
(结合传智资料整理)
- UI基础style样式&Theme
- UI基础style样式&Theme
- android样式和主题(style&theme)
- android基础---样式和主题(style&theme)
- 【Android UI】theme style
- 样式Style和主题Theme
- Android基础之Style Theme
- 自定义样式(style)与主题(theme)
- android的样式(style)与主题(theme)
- android的样式(style)与主题(theme)
- Android中的主题(theme)和样式(style)
- Android样式和主题(style&theme)
- android的样式(style)与主题(theme)
- android的样式(style)与主题(theme)
- android style(样式)和theme(主题)设置
- android样式和主题(style&theme)
- android的样式(style)与主题(theme)
- android的样式(style)与主题(theme)
- Android请求root权限
- 也学微博开发(二)联网登录验证之使用外部jar
- 也学微博开发(三)修正--认证登录
- 也学微博开发(四)-----保存认证信息、菜单栏和微博首页
- bundle实现Activity之间的数据传递
- UI基础style样式&Theme
- 编程艺术算法题
- android基础AlertDialog使用
- Hello, Views(一)Gallery滑动的图片(附源码下载)
- Hello,Views(二)GoogleMap地图使用上篇(附源码)
- Hello,Views(三)GoogleMap地图使用下篇(附源码)
- Hello,Views(四)用Web View实现浏览网页(附源码)
- Hello,Views(五)自动填充文字AutoCompleteTextView(附源码)
- Hello,Views(六)下拉框Spinner(附源码)