您的位置:首页 > 移动开发 > Android开发

Android 下style和theme的用法

2015-03-17 19:29 239 查看
  Android中的style可以被多个界面共用,所以它适合为应用做一些基础的外观设置,可以使应用的外观既统一又容易修改。style的用法为:1.在values文件夹下新建一个style.xml文件2.在style.xml文件中来设置android各个组件的属性。style节点有三个属性值,分别是
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

<style name="content_demo">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textColor">#ff2376</item>
<item name="android:textSize">30sp</item>
<item name="android:background">#448811</item>

</style>

<style name="content_demo_child" parent="content_demo">
<item name="android:textColor">#000000</item>
</style>
</resources>
name   item 和 parent.name属性来设置style的名称;item属性来设置Android组建的属性;parent属性来设置style之间的继承关系,也就是说一个style可以完全得到另一个style的全部设置。并且子style可以复写父style的属性。3.在layout.xml中,给需要使用样式的组建引入所需的style即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
style="@style/content_demo"
android:text="我是第一个"
/>
<TextView
style="@style/content_demo"
android:text="我是第二个"
/>
<TextView
style="@style/content_demo"
android:text="我是第三个"
/><TextView
style="@style/content_demo_child"
android:text="我是第四个"
/>

</LinearLayout>
theme和style没有什么区别,一个使用上比较大的区别就是,style主要是用来修饰Android的View的,而theme则主要是用来修饰Android的activity的。
如果想让某个主题修饰整个应用中所有的activity,只要把该theme设置到AndroidManifest.xml文件中的application节点下即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: