这篇文章主要介绍“Android中Style和Theme有什么作用”,在日常操作中,相信很多人在Android中Style和Theme有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中Style和Theme有什么作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Style和Theme主要是用于对Android应用进行美化设计,优化交互页面,充分利用各种样式和主题资源,可以开发出各种风格的Android应用
Style
当我们需要面对大量的组件指定相似格式,例如:字体,颜色,背景颜色等等,要是我们每次都选择为View组件重复指定这些属性,无疑会造成工作量的剧增,而且不理由代码维护性和可视性。
类似于Word和PPT的管理格式:一个样式等于一组格式的集合,如果设置某段文本使用某个样式,那么该样式的所有格式会整体被应用于该文本。一个样式相对于多个格式的集合,其他UI组件通过style属性来指定样式,这就相当于把该样式包含的所有格式同时应用于该UI组件
定义样式资源:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="style1">
<item name="android:textSize">11sp</item>
<item name="android:textColor">#32d</item>
</style>
<style name="style2" parent="@style/style1">
<item name="android:background">#116</item>
<item name="android:padding">26dp</item>
<item name="android:textColor">#005</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="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/style1"
style="@style/style1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/style2"
style="@style/style2"/>
</LinearLayout>
Theme
和样式资源很相似,主题资源的XML也放在和样式资源一起的目录下
但是主题资源和样式资源还真就有这区别:主题不能作用于单个View组件,主体应该对整个应用中的所有Activity起作用,或者对指定的Acitivity起作用。主题定义的格式应该是改变窗口外观的格式,例如窗口标题,窗口边框这些等
定义主题资源:
<style name="CrazyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowFrame">@drawable/window_border</item>
<item name="android:windowBackground">@drawable/star</item>
</style>
在Android代码中写入:
public vod onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(R.style.CrazyTheme);
setContentView(R.layout.main);
}
最后在AndroidManifest.xml中添加theme即可
<application android:theme="@style/CrazyitTheme">
....
到此,关于“Android中Style和Theme有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69917874/viewspace-2695844/