现在的位置: 首页 > 综合 > 正文

android theme相关待整理

2017年12月18日 ⁄ 综合 ⁄ 共 4670字 ⁄ 字号 评论关闭

1.android theme相关讲解 http://blog.csdn.net/sshhbb/article/details/7219838

        
http://blog.csdn.net/notice520/article/details/6171632
      

学习中碰到的问题:

1.application 层 android:theme="@style/AppTheme"  activity 层 theme有啥作用?

教训在activity中间添加android:theme="@android:style/Theme 可以得到老版本的optionsMenu效果

首先,style和theme都是资源,android提供了很多这样的默认资源。你可以来使用它们。同时你也可以自己定义style和theme。这非常的简单,只需要在res/values/这个路径里面新建一个.xml文件,而且他的根节点必须是<resources>.对每一个style和theme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性,我们写的style和theme就会继承这个父类的属性。style和theme的定义格式相同。不过style是针对view来说的,
比如TextView,EditText这些,而theme必须针对整个activity或者整个程序,你必须在AndroidManifest.xml中的<application>或者<activity>中定义。 
先来看看style,比如如下一段代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>    

<mce:style name="CodeFont" parent="@android:style/TextAppearance.Medium">

<!--

<item name="android:layout_width">fill_parent</item>        

<item name="android:layout_height">wrap_content</item>       

<item name="android:textColor">#00FF00</item>  

<item name="android:typeface">monospace</item>    

--></mce:style>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium" mce_bogus="1">     

<item name="android:layout_width">fill_parent</item>  

<item name="android:layout_height">wrap_content</item>    

<item name="android:textColor">#00FF00</item>    

<item name="android:typeface">monospace</item>  

</style>

</resources>

可以看到这个style的名字为CodeFont。 parent后面就是父类的style, CodeFont继承这个父类的属性。可以看到这个父类的style是android中默认的,你也可以继承你自定义的style,这时候不需要再写parent属性,而是使用ContFont.red这样的方式,而且你可以继续继承,写成ContFont.red.small。接下来每一个item定义一个属性。定义属性的最好方法就是在api文档里找到这个view的xml属性,比如在EditText中有InputType这个属性,那么在你的style里面你就可以来定义它。
 
这样一个style就写好了。
使用也非常简单,我们只要在写我们的view时,加入style标签就可以了,就像这样

<TextView    style="@style/CodeFont" mce_style="@style/CodeFont"    android:text="@string/hello" />  
面讲讲主题,前面已经说了。主题需要在AndroidManifest.xml中注册。如果你想整个程序都使用这个主题,你可以这样写

<application android:theme="@style/CustomTheme">  

如果你只需要在某个Activity中使用主题,那么只要在Activity标签中写入android:theme=就可以了,android有很多好的默认主题,比如

<activity android:theme="@android:style/Theme.Dialog">  

这就会使你的整个Activity变成一个对话框形式,或者,如果你希望背景是透明的,可以这样写

<activity android:theme="@android:style/Theme.Translucent">  
 
同样的我们也可以继承父类theme,写法和style一样,就不赘述了。当然,和style一样,你也可以自己定义一个theme,写个例子

<?xml version="1.0" encoding="utf-8"?>
<resources> 

<mce:style name="CustomTheme">

<!-- 

 <item name="android:windowNoTitle">true</item>

 <item name="windowFrame">@drawable/screen_frame</item>

 <item name="windowBackground">@drawable/screen_background_white</item> 

 <item name="panelForegroundColor">#FF000000</item> 

 <item name="panelBackgroundColor">#FFFFFFFF</item>

 <item name="panelTextColor">?panelForegroundColor</item>

 <item name="panelTextSize">14</item> 

 <item name="menuItemTextColor">?panelTextColor</item> 

 <item name="menuItemTextSize">?panelTextSize</item>  

--></mce:style>

<style name="CustomTheme" mce_bogus="1">

 <item name="android:windowNoTitle">true</item> 

 <item name="windowFrame">@drawable/screen_frame</item>

 <item name="windowBackground">@drawable/screen_background_white</item>

 <item name="panelForegroundColor">#FF000000</item>

 <item name="panelBackgroundColor">#FFFFFFFF</item>

 <item name="panelTextColor">?panelForegroundColor</item>

 <item name="panelTextSize">14</item>

 <item name="menuItemTextColor">?panelTextColor</item> 

<item name="menuItemTextSize">?panelTextSize</item> 

 </style>

</resources>  
 
如果你要在java代码中加载主题的话,只要用setTheme(R.style.CustomTheme)就可以了,不过记得一定要在初始化任何view之前,比如一定要放在我们常用的setContentView()之前。通常,我们不建议这么做。
在写程序布局的时候,熟练的使用style和theme是非常必要和有益的

系统自带theme

<span style="font-size:16px;">

•android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式

•android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏

•android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏

•android:theme="@android:style/Theme.Light"  背景为白色

•android:theme="@android:style/Theme.Light.NoTitleBar"  白色背景并无标题栏 

•android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"  白色背景,无标题栏,全屏

•android:theme="@android:style/Theme.Black"  背景黑色

•android:theme="@android:style/Theme.Black.NoTitleBar"  黑色背景并无标题栏

•android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"    黑色背景,无标题栏,全屏

•android:theme="@android:style/Theme.Wallpaper"  用系统桌面为应用程序背景

•android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  用系统桌面为应用程序背景,且无标题栏

•android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"  用系统桌面为应用程序背景,无标题栏,全屏

•android:theme="@android:style/Translucent" 半透明效果

•android:theme="@android:style/Theme.Translucent.NoTitleBar"  半透明并无标题栏

•android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"  半透明效果,无标题栏,全屏

•android:theme="@android:style/Theme.Panel"

•android:theme="@android:style/Theme.Light.Panel"

</span>

具体展示见 http://stephen830.iteye.com/blog/1129203

    http://android.blog.51cto.com/268543/303728

疑问 ?android:textAppearanceMedium 前面?表示什么意思?

抱歉!评论已关闭.