`

Android样式与主题

 
阅读更多
android样式,主题设置心得 http://zhy584520.iteye.com/blog/1168227
1. android style样式的设置.先看下面一段设置字体与文字颜色的样式文件
<resources>
 <style name="normal">
  <item name="android:textSize">30px</item>
  <item name="android:textColor">#FF00FF</item>
 </style>
</resources>

这样我们就可以在view上进行设置了。设置方法style="@style/normal".
请注意:item项里的name属性的值是android:textSize,于是很好奇这个是从哪来的呢(在开发中没有提示的),其实它是系统中android.R.attr下默认的(系统有的,你设置才有效,不过也得你的view有这个属性才行,缺一不可).你可以在一个继承activity的类中的方法中输入android.R.attr.就有提示了,如(android.R.attr.textSize)你就可以把这个Copy到样式文件中使用了,这样也比较少的机率弄错,也不用记这些东西了。这样是不是很好呢?

2.android theme设置,例如设置一些acvitity全屏,无状态栏,通明度等设置,可以在theme通一设置,并应用与
需要设置的activity,这个需在在AndroidManifest.xml中对其设置,android:theme="@style/full_screen"
主题样式:
<resources>
 <style name="full_screen" parent="android:Theme.Black">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">?android:windowNoTitle</item>
 </style>
</resources>

同样的,item项中的name属性值是来源于android系统android.R.attr.windowNoTitle。不过得注意style里parent属性值是android系统自带的主题,这些值是来源于android.R.style.Theme_Black,这里的android.R.style.Theme_Black在主题文件里设置是对应android:Theme.Black,所有的其它自带主题都是差不多这样设置。_换成.就OK。

Android样式 http://www.eoeandroid.com/thread-18928-1-1.html
android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。

在Android中可以这样定义样式:
在res/values/styles.xml文件中添加以下内容
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name=“microchen”> <!-- 为样式定义一个全局唯一的名字--> 
        <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 --> 
        <item name="android:textColor">#0000CC</item> 
    </style> 
</resources> 


在layout文件中可以像下面这样使用上面的android样式:
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....> 
    <TextView style="@style/microchen" 
        .....  /> 
</LinearLayout> 

<style>元素中有一个parent属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,如下:
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="microchen"> 
        <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 --> 
        <item name="android:textColor">#0000CC</item> 
    </style> 
    <style name="submicrochen" parent="@style/microchen"> 
        <item name="android:textColor">#FF0000</item> 
    </style> 
</resources>


android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name=“microchenTheme"> 
<item name=“android:windowNoTitle”>true</item> <!– 没标题  
<item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全屏显示  
</style> 
</resources> 

上面“?android:windowNoTitle”中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:
<application android:icon="@drawable/icon" android:label="@string/app_name" 
     android:theme="@style/microchenTheme"> 
   ...... 
</application> 


除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.microchenTheme); 

尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。

另外android系统也定义了一些主题,例如:<activity android:theme=“@android:style/Theme.Dialog”>,该主题可以让Activity看起来像一个对话框,如果需要查阅这些主题,可以在文档的referenceàandroid-->R.style 中查看。

应用界面主题Theme使用方法 http://my.eoe.cn/zhou1229/archive/4432.html
主题Theme就是用来设置界面UI风格,可以设置整个应用或者某个活动Activity的界面风格。在Android SDK中内置了下面的Theme,可以按标题栏Title Bar和状态栏Status Bar是否可见来分类:
•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"


这些主题可以应用到整个应用Application范围或者某个活动Activity范围中。

应用Application范围
在AndroidManifest.xml中的application节点中设置theme属性,主题theme应用到整个应用程序中。

活动Activity范围
使用java代码或者在AndroidManifest.xml中对活动Activity的主题进行设置,主题仅应用到当前活动中。
在AndroidMainifest.xml设置方法:
使用java代码进行设置,在当前活动Activity的onCreate中进行设置:
@Override
public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setTheme(android.R.style.Theme_Translucent_NoTitleBar);
   setContentView(R.layout.main);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics