Android 自定义主题和风格

  android中可以自定义主题和风格
  风格,也就是style,我们可以将一些统一的属性拿出来,比方说,长,宽,字体大小,字体颜色等等。
可以在res/values目录下新建一个styles.xml的文件,在这个文件里面有resource根节点,在根节点里面添加item项,item项的名字就是属性的名字,item项的值就是属性的值,如下所示:
  
 
  
1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2 < resources >
3 < style name = " MyText " parent = " @android:style/TextAppearance " >
4 < item name = " android:textColor " > # 987456 </ item >
5 < item name = " android:textSize " > 24sp </ item >
6 </ style >
7 </ resources >
style中有一个父类属性parent, 这个属性是说明当前的这个style是继承自那个style的,
当然这个style的属性值中都包含那个属性中的,你也可以修改继承到的属性的值,好了,style完成了,我们
可以测试一下效果了,先写一个布局文件,比如说一个TextView什么的,可以用到这个style的。这里我就写一个
EditText吧。下面是布局文件:
 
  
1 <? xml version="1.0" encoding="utf-8" ?>
2 < LinearLayout
3 xmlns:android ="http://schemas.android.com/apk/res/android"
4 android:layout_width ="match_parent"
5 android:layout_height ="match_parent" >
6
7 < EditText
8 android:id ="@+id/myEditText"
9 android:layout_width ="match_parent"
10 android:layout_height ="match_parent"
11 style ="@style/MyText"
12 android:text ="测试一下下" />
13 </ LinearLayout >
下面我们在代码中引用这个布局文件,看一下是不是我们要的style的效果。
看效果:
2011062011202028.png
恩,这里面的字体和颜色都是我们在style里面设置的,比方说有很多要统一字体大小和颜色的,我们就可以用到style了,还是很方便的。
当然系统里面有很多style的,具体什么效果我就不多说了,大家可以试试看。

  说完了style,下面就说说Theme,Theme跟style差不多,但是Theme是应用在Application或者Activity里面的,
而Style是应用在某一个View里面的,还是有区别的,好了,废话不多说,还是看代码吧。
下面的是style文件:
 
  
1 <? xml version="1.0" encoding="utf-8" ?>
2 < resources >
3 < style name ="MyText" parent ="@android:style/TextAppearance" >
4 < item name ="android:textColor" > #987456 </ item >
5 < item name ="android:textSize" > 24sp </ item >
6 </ style >
7 < style parent ="@android:style/Theme" name ="CustomTheme" >
8 < item name ="android:windowNoTitle" > true </ item >
9 < item name ="android:windowFrame" > @drawable/icon </ item >
10 < item name ="android:windowBackground" > ?android:windowFrame </ item >
11 </ style >
12 </ resources >
可以看到这里写了一个继承自系统默认的Theme的主题,里面有3个属性,这里强调一下第三个属性的值的问题,
这里打个问号,然后加前面的一个item的名字表示引用的是那个名字的值,也就是那个名字对应的图片。

然后我们在Manifest.xml里面的Application里面加一个Theme的属性,这个属性对应的就是我们上面写的Them。
 
  
1 < application android:icon ="@drawable/icon" android:label ="@string/app_name"
2 android:theme ="@style/CustomTheme" >
3 < activity android:name =".TestStyle"
4 android:label ="@string/app_name" >
5 < intent-filter >
6 < action android:name ="android.intent.action.MAIN" />
7 < category android:name ="android.intent.category.LAUNCHER" />
8 </ intent-filter >
9 </ activity >
完了之后我们运行一下,看下是不是我们想要的效果。
2011062011414949.png
我们可以看到,没有标题栏,背景和fram都是我们设置的图片。
当然也可以在代码中设置主题:
如下:
 
  
1 package com.test.shang;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5
6 public class TestStyle extends Activity {
7
8 @Override
9 protected void onCreate (Bundle savedInstanceState) {
10 super .onCreate(savedInstanceState);
11
     setTheme(R.style.CustomTheme);
12      setContentView(R.layout.test_style);
13 }
14 }
好了,就到这里吧。


转载于:https://www.cnblogs.com/shang53880/archive/2011/06/20/2085102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值