A. 如何在 android 应用程序中使用自定义主题
在Android中,定义外观最简单的方式是直接设置属性在视图对象上。由于要对整个应用程序设置样式,这种方法就显得不是很方便了。因此,我们可以创建
样式来绑定视图属性。但要注意的是,样式只能设置在xml里。这意味着,我们在创建/加载视图的时候,只能一次设置一个样式。下面是如何在res
/values/styles.xml文件中给TextView设置属性的示例。
<style name="CustomText" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/TextAppearance.Medium">
<item name="android:textSize">20sp</item> <item name="android:textColor">#008</item>
</style>
我们引用@style/CustomText样式应用在我们的layout.xml文件中。该样式引用了一个父样式
@android:style/TextAppearance.Medium。由于开始样式引自android: 命名空间,那么我们这个样式默认也随android平台。
创建主题绑定样式
假设我们要改变我们所有TextView的文字大小和颜色,并且不需要明确设置每一个TextView。这是一个非常常见的情况,幸运的
是,Android提供了一个非常强大的主题机制。从本质上讲,主题就是样式资源本身,使用“key”指向具体的样式。一个视图对象基于这个key可以查
看到具体的样式。下面是一个简单的例子,在res/values/themes.xml文件:
<style name="Theme.MyApp" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/Theme.Holo">
<item name="android:textAppearance">@style/CustomText</item>
</style>
如上所述,主题是样式资源本身,所以我们声明一个Theme.App继承自Android平台提供的holo主题。我们将我们的CustomText样式
指定到android:textAppearance属性。属性就是一个预定义的“变量”,它可以被其他资源元素引用。事实上,它也可以创建自定义属性在
res/values/attr.xml文件。
现在有趣的事情来了。我们的主题Theme.MyApp不仅可以通过AndroidManifest.xml的设置应用到Activity上,它也可以在
运行时在代码中设置。你将不得不重新启动当前Activity(或使用ContextWrapper应用UI的主题部分),但它使主题更加动态。
B. android怎样优雅的动态改变整个application的theme
Android中实现theme主题可以使用在activity中使用setTheme(int)的方法,SDK中对此方法的说明为:
//Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling android.app.Activity.setContentView or android.view.LayoutInflater.inflate).
//需要在setcontentview函数或者inflate函数之前使用。
效果图如下:
实现步骤:
首先需要定义一个属性,此属性用于赋值给控件的属性,相当于控件属性值的“变量”。
在attrs.xml中,定义三个属性,属性的format均为reference|color
<resources>
<attr name="button_bg" format="reference|color" />
<attr name="activity_bg" format="reference|color" />
<attr name="text_cl" format="reference|color" />
</resources>
接下来,在styles.xml中,编写自定义的Theme
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="text_cl">#ffffff</item>
<item name="button_bg">#000000</item>
<item name="activity_bg">#ffffff</item>
</style>
<style name="DarkTheme" parent="AppBaseTheme">
<item name="text_cl">#000000</item>
<item name="button_bg">#ffffff</item>
<item name="activity_bg">#000000</item>
</style>
选择一种模式作为程序的默认theme,注意:由于我是在layout布局文件中定义的view的样式,因此,为了保证theme切换时不会出现找不到资源的问题,因此需要在每一种用到的自定义theme中,都加上item。这里的item如text_cl和view的textColor属性的format是一致的。
Android manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testandroidsettheme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:name="com.example.testandroidsettheme.app.MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testandroidsettheme.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
C. Android系统自带样式Theme总结
本文从网上复制,给自己做笔记的,摘自: http://blog.csdn.net/hongya1109110121/article/details/11985545
Android系统自带样式(@android:style/) (转)
1 android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"
布局页面最上面 不会显示 android:icon="@drawable/ic_launcher"中的值和android:label="@string/app_name"的值。
2 android:theme="@style/AppTheme"
布局页面最上面 显示 android:icon="@drawable/ic_launcher"中的值和android:label="@string/app_name"的值。
其他
在AndroidManifest.xml文件的activity中配置
1、android:theme="@android:style/Theme"
默认状态,即如果theme这里不填任何属性的时候,默认为Theme
2、android:theme="@android:style/Theme.NoDisplay"
任何都不显示。比较适用于只是运行了activity,但未显示任何东西
3、android:theme="@android:style/Theme.NoTitleBar“
背景主题的没有标题栏的样式,默认如果没有设置的话,显示黑背景
4、android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
背景主题的没有标题栏且全屏的样式,默认为黑背景
5、android:theme="@android:style/Theme.Black"
默认状态下黑背景
6、android:theme="@android:style/Theme.Black.NoTitleBar"
黑背景主题的没有标题栏的样式
7、android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
黑背景主题的没有标题栏且全屏的样式
8、android:theme="@android:style/Theme.Light"
默认状态下亮背景,与上述黑背景Theme.Black相反
9、android:theme="@android:style/Theme.Light.NoTitleBar"
亮背景主题的没有标题栏的样式,与Theme.Black.NoTitleBar相反
10、android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
亮背景主题的没有标题栏且全屏显示的样式,与Theme.Black.NoTitleBa.Fullscreenr相反
11、android:theme="@android:style/Theme.Dialog"
对话框样式 将整个activity变成对话框样式出现
12、android:theme="@android:style/Theme.InputMethod"
Window animations that are applied to input method overlay windows
13、android:theme="@android:style/ Theme.Panel"
删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且此为默认的样式
14、android:theme="@android:style/ Theme.Light.Panel"
删除掉所有多余的窗口装饰,在一个空的矩形框中填充内容,作用范围相当于把dialog中的所有元素全部去掉,只是一个空的矩形框,且默认是light的样式
15、android:theme="@android:style/Theme.Wallpaper"
使用墙纸做主题,默认状态。
16、android:theme="@android:style/ Theme.WallpaperSettings"
使用墙纸做主题,默认是使用将上一个界面调暗之后作为主题
17、android:theme="@android:style/ Theme.Light.WallpaperSettings"
使用墙纸做主题,默认Light状态
18、android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
使用墙纸做主题,且没有标题栏
19、android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
使用墙纸做主题,且没有标题栏,且全屏显示
20、android:theme="@android:style/Theme.Translucent"
半透明状态下的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。
21、android:theme="@android:style/Theme.Translucent.NoTitleBar"
半透明状态下没有标题栏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。
22、android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
半透明状态下没有标题栏且全屏的背景,将运行此activity之前的屏幕作为半透明状态作为此activity运行时的样式。
摘自: http://blog.csdn.net/hongya1109110121/article/details/11985545
D. android选用apptheme,怎样全屏
如果你想让这个app启动的时候,是全屏显示的话,你可以这样做:
打开工程目录下的AndroidManifest.xml文件,双击后选择位于Instrumentation右边的AndroidManifest.xml选项框
在<activity>节点下找到类似android:name=".MainActivity"的代码
在该段代码的下一行加上如下代码即可:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
最终的效果如下:
E. 如何在 Android 应用程序中使用自定义主题
在Android中,定义外观最简单的方式是直接设置属性在视图对象上。由于要对整个应用程序设置样式,这种方法就显得不是很方便了。因此,我们可以创建 样式来绑定视图属性。但要注意的是,样式只能设置在xml里。这意味着,我们在创建/加载视图的时候,只能一次设置一个样式。下面是如何在res /values/styles.xml文件中给TextView设置属性的示例。 <style name="CustomText" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/TextAppearance.Medium"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> 我们引用@style/CustomText样式应用在我们的layout.xml文件中。该样式引用了一个父样式 @android:style/TextAppearance.Medium。由于开始样式引自android: 命名空间,那么我们这个样式默认也随android平台。 创建主题绑定样式 假设我们要改变我们所有TextView的文字大小和颜色,并且不需要明确设置每一个TextView。这是一个非常常见的情况,幸运的 是,Android提供了一个非常强大的主题机制。从本质上讲,主题就是样式资源本身,使用“key”指向具体的样式。一个视图对象基于这个key可以查 看到具体的样式。下面是一个简单的例子,在res/values/themes.xml文件: <style name="Theme.MyApp" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/Theme.Holo"> <item name="android:textAppearance">@style/CustomText</item> </style> 如上所述,主题是样式资源本身,所以我们声明一个Theme.App继承自Android平台提供的holo主题。我们将我们的CustomText样式 指定到android:textAppearance属性。属性就是一个预定义的“变量”,它可以被其他资源元素引用。事实上,它也可以创建自定义属性在 res/values/attr.xml文件。 现在有趣的事情来了。我们的主题Theme.MyApp不仅可以通过AndroidManifest.xml的设置应用到Activity上,它也可以在 运行时在代码中设置。你将不得不重新启动当前Activity(或使用ContextWrapper应用UI的主题部分),但它使主题更加动态。
F. Android 的style和theme
例子
Theme是针对窗体级别的,改变窗体样式;
Style是针对窗体元素级别的,改变指定控件或者Layout的样式
简单的说就是Theme里面有包含了好多好多Style
常用于Toolbar下
比如
android:theme="@style/AppTheme.AppBarOverlay设置了Toolbar的主题
app:popupTheme="@style/AppTheme.PopupOverlay"设置了Toolbar节点下的view的主题
默认情况下我们使用theme为android:Theme.Holo.Light.DarkActionBar那么ActionBar文字是白的,ActionBar Overflow弹出的是黑底白字,如果需求是白底黑字那么只要设置toolbar的app:popupTheme="ThemeOverlay.AppCompat.Light"
附上一张官方的
装载自 关于Android的style和theme
G. 怎么设置android背景颜色
通常来说,每个界面都对应一个activity。而在activity的View视图中,可以在最外层容器去设置背景图片或背景颜色。
在xml布局里:
android:background="@drawable/img1"
或者
android:background="@color/white"
在java代码里,也可以设置
1 layout.setBackgroundColor(R.color.white);
2 layout.setBackgroundDrawable(drawable);
3 layout.setBackgroundResource(R.drawable.img1);
再者,系统默认的背景色是能过theme来控制的,就是说创建一个activity的背景色,如果在
AndroidManifest.xml文件里有设置如下:
android:theme="@android:style/Theme"
这样设置activity的主题样式,"@android:style/Theme"一般是系统默认的。这个不单是背景色,还有其它的样式,具体可以在网上查一下android:theme的用法。
而"@android:style/Theme"的背景色就是黑色。
H. android 内置theme有哪些
Android系统中自带样式分享,需要的朋友可以参考下
android:theme="@android:style/Theme.Dialog" : Activity显示为对话框模式
android:theme="@android:style/Theme.NoTitleBar" : 不显示应用程序标题栏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" : 不显示应用程序标题栏,并全屏
android:theme="Theme.Light ": 背景为白色
android:theme="Theme.Light.NoTitleBar" : 白色背景并无标题栏
android:theme="Theme.Light.NoTitleBar.Fullscreen" : 白色背景,无标题栏,全屏
android:theme="Theme.Black" : 背景黑色
android:theme="Theme.Black.NoTitleBar" : 黑色背景并无标题栏
android:theme="Theme.Black.NoTitleBar.Fullscreen" : 黑色背景,无标题栏,全屏
android:theme="Theme.Wallpaper" : 用系统桌面为应用程序背景
android:theme="Theme.Wallpaper.NoTitleBar" : 用系统桌面为应用程序背景,且无标题栏
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" : 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="Theme.Translucent : 透明背景
android:theme="Theme.Translucent.NoTitleBar" : 透明背景并无标题
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" : 透明背景并无标题,全屏
android:theme="Theme.Panel ": 面板风格显示
android:theme="Theme.Light.Panel" : 平板风格显示
I. android l theme怎么用
1,用 android l的sdk
2,在res/values/styles.xml 里边声明 (具体网络一下)
J. android theme 和stytle的区别
Theme 和Style资源,将一些通用的属性提取出来,方便使用。
相同:
Theme和Style位于values文件夹下styles.xml下,格式相同。
[java] view plain
<style name="MyTransparent">
<item name="android:windowBackground">@color/transparent_background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<item name="Android:windowNoTitle">?android:windowIsTranslucent</item>其中 问号表示引用本主题中已经定义过的资源的值。
在需要使用的时候调用
[java] view plain
<EditText
android:id="@+id/nameEdit"
<span style="color:#ff0000;"> style="@style/LoginOrRegisterEditStyle"</span>
android:hint="@string/input_real_name"
android:layout_below="@id/tipHead"
android:layout_marginTop="20dp" />
不同:
Theme在AndroidManifest.xml中德Application节点或者Activity节点设置android:theme,或者在对应Activity中通过代码设置setTheme(),影响整个应用或者一个Activity.
Style一般是在布局中的View中设置。影响单个View,如EditText,TextView等等。
如果主题和样式属性有冲突,样式的属性高于主题。
如果需要使用系统自带的主题
[java] view plain
android:theme=<span style="color:#ff0000;">"@android:style</span>/Theme.Light.NoTitleBar"