博观而约取,厚积而薄发。这篇文章主要讲述Android app与Activity主题配置相关的知识,希望能为你提供帮助。
一、样式和主题(style and theme)
1.1 样式(style)是来指定视图和窗口的外观和格式的一组属性集合。样式可以指定文本、字体及大小、背景颜色等属性。比如:
1 < resources> 2 3< style name="customfont"> 4< item name="android:layout_width"> match_parent< /item> 5< item name="android:layout_height"> wrap_content< /item> 6< item name="android:textColor"> #d05050< /item> 7< item name="android:textSize"> 16sp< /item> 8< item name="android:gravity"> center< /item> 9< /style> 10 11 < /resources>
样式在TextView控件中使用:
1 < TextView 2android:id="@+id/show_activity_id" 3style="@style/customfont" 4android:text="hello"/>
1.2 样式继承
style可以通过paren属性继承一个现在的样式,然后,修改或者添加属性。比如:
1 < resources> 2 3< style name="customfont"> 4< item name="android:layout_width"> match_parent< /item> 5< item name="android:layout_height"> wrap_content< /item> 6< item name="android:textSize"> 16sp< /item> 7< item name="android:gravity"> center< /item> 8< /style> 9 10< style name="customfontcolor" parent="customfont"> 11< item name="android:textColor"> #d05050< /item> 12< /style> 13 14 < /resources>
当前,在继承自已定义样式时,也可以不需要parent属性,只需要在新的样式名称前,加上将要继承样式的名称,名称间用“ .” 分隔。比如:
1 < resources> 2 3< style name="customfont.customfontcolor"> 4< item name="android:textColor"> #d05050< /item> 5< /style> 6 7 < /resources>
1.2 为Activity 或者 Application 设置主题
为Activity或者Application设置主题,在Androidmanfest.xml文件中,在< Activity> (或者< Application> )标签中,设置Android:theme属性。比如:
1 < application 2android:allowBackup="true" 3android:icon="@mipmap/ic_launcher" 4android:label="@string/app_name" 5android:supportsRtl="true" 6android:theme="@style/AppTheme"> 7 8< activity 9android:name=".Activity.SecondActivity" 10android:launchMode="singleTop" 11android:theme="@style/customttheme"/> 12 13 < /application>
假如,继承的系统主题并不符合要求,也可以继承这个主题,修改或者添加属性,如下:
< resources> < style name="customttheme" parent="@style/AppTheme"> < item name="android:colorBackground"> #666< /item> < /style> < /resources>
【Android app与Activity主题配置】
推荐阅读
- Android ViewGroup
- Android PendingIntent的使用
- 值得推荐的android开发框架简单介绍
- Android Activity属性
- 一步一步教你在 Android 里创建自己的账号系统
- Android Broadcast Receiver (广播接收者)
- Android Layout布局
- Android ViewPager组件
- 如何使用Helm命令(语法和用法示例)