Ste*_* J. 14 android themes coding-style android-manifest
我有一个Android清单,其中包含我想要应用于样式的活动:
<activity android:label="@string/app_name" android:name="Language" android:theme="@android:style/Theme.NoTitleBar>
Run Code Online (Sandbox Code Playgroud)
它现在看起来如何,但在保留NoTitleBar属性的同时,我也希望添加此属性:
android:style/Theme.Light"
Run Code Online (Sandbox Code Playgroud)
但我对Android这么新,我无法弄明白.
请帮忙!
Jos*_*arl 35
您的清单中不能同时应用多个主题.
我相信有一个主题Theme.Light.NoTitleBar
会做你想要的 - 但我会告诉你如何自己轻松地做到这一点并定制更多.
你需要做的是创造出具有无论是主题Theme.NoTitleBar
还是Theme.Light
因为它的母公司和定制你想要的位-在这种情况下,最简单的方法是创建一个主题,Theme.Light
因为它的父,只是隐藏标题栏(而不是有Theme.NoTitleBar
作为父母,然后必须使一切光更难!).
您可以使用themes.xml
文件values
夹中文件中的以下代码执行此操作:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- use the Android Light theme as the base for our own theme -->
<style name="MySuperTheme" parent="@android:style/Theme.Light">
<!-- hide the Window Title -->
<item name="android:windowNoTitle">true</item>
<!-- You could change the scrollbar, checkbox style, anything! -->
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后android:theme="@style/MySuperTheme"
用于您的活动(或者您甚至可以将它应用于整个应用程序,方法是将它放在应用程序元素上 - 如果您将样式应用于单个活动并为整个应用程序设置一个集合,那么个人的样式活动将是显示的那个).
查看Android themes.xml,了解您可以在自己的主题中自定义的所有内容的列表.
您还可以查看所有Android样式,了解它们是如何完成的.
您将需要至少 2 种样式,最好从基本样式继承,例如Theme.Material
变体,或者如果您使用appcompat
则Theme.AppCompat
变体。在每种样式中,用特定于主题的值覆盖颜色、可绘制等值。
值/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="android:textColorPrimary">#FFFFFF</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="android:textColorPrimary">#000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如果您仅在布局中使用框架或appcompat
属性(例如等) colorAccent
,这就足够了。android:textColorPrimary
但如果您需要自己的属性(例如,每个主题的颜色不同的可绘制对象),那么您将需要定义自定义属性。
值/attrs.xml
<attr name="themedMenuStoryDrawable" format="reference" />
<attr name="themedMenuCommentDrawable" format="reference" />
...
Run Code Online (Sandbox Code Playgroud)
为您的自定义属性指定特定于主题的值:
值/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="themedMenuStoryDrawable">@drawable/ic_subject_white_24dp</item>
<item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_white_24dp</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="themedMenuStoryDrawable">@drawable/ic_subject_black_24dp</item>
<item name="themedMenuCommentDrawable">@drawable/ic_mode_comment_black_24dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
?attr/
然后在布局、菜单等中引用带有前缀的自定义属性:
菜单/my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/menu_comment"
android:icon="?attr/themedMenuCommentDrawable" />
<item android:id="@id/menu_story"
android:icon="?attr/themedMenuStoryDrawable" />
</menu>
Run Code Online (Sandbox Code Playgroud)
查看我的博客文章以获取完整指南。
归档时间: |
|
查看次数: |
20542 次 |
最近记录: |