Android 中的Theme和Style使用

Android 中的Theme和Style使用,还是比较简单的。

1、 首先在res/values/styles.xml的resource中定义三个样式,分别为:

<style name= "TextView" >
   <item name= "android:textSize" >38sp</item>
   <item name= "android:textColor" ># 128 </item>
   <item name= "android:shadowRadius" > 1.0 </item>
   <item name= "android:background" ># 035 </item>
  </style>
 
  <style name= "EditText" >
   <item name= "android:shadowColor" > @android :color/black</item>
   <item name= "android:shadowRadius" > 1.0 </item>
   <item name= "android:background" ># 312 </item>
   <item name= "android:foreground" ># 432 </item>
   <item name= "android:textAppearance" >?android:attr/textAppearanceMedium</item>
   <item name= "android:height" >80dp</item>
  </style>
 
<style name= "Button" >
     <item name= "android:background" > @android :drawable/edit_text</item>
     <item name= "android:textAppearance" >?android:attr/textAppearanceMedium</item>
</style>

 然后在res/layout文件下的activity_main.xml中的控件中引用刚才定义的Style。

<TextView
     android:id= "@+id/textView1"
     style= "@style/TextView"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text= "@string/hello_world"  />
 
<Button
     android:id= "@+id/button1"
     style= "@style/Button"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:layout_alignLeft= "@+id/textView1"
     android:layout_below= "@+id/editText1"
     android:layout_marginTop= "18dp"
     android:text= "@string/hello_world"  />
 
<EditText
     android:id= "@+id/editText1"
     style= "@style/EditText"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:layout_alignLeft= "@+id/button1"
     android:layout_below= "@+id/textView1"
     android:layout_marginTop= "19dp"
     android:ems= "10"
     android:text= "@string/hello_world"  />

 

2、Android的Theme的使用

首先在res/values/themes.xml中定义Theme。

<?xml version= "1.0"  encoding= "utf-8" ?>
<resources xmlns:android= "http://schemas.android.com/apk/res/android" >
     <style name= "Theme"  parent= "android:Theme.Light" >
         <item name= "android:windowFullscreen" > true </item>
         <item name= "android:windowTitleSize" >60dip</item>
         <item name= "android:windowTitleStyle" > @style /WindowTitle</item>
         <item name= "android:background" ># 234 </item>
     </style>
     <style name= "WindowTitle" >
         <item name= "android:singleLine" > true </item>
         <item name= "android:shadowColor" ># 658 </item>
         <item name= "android:shadowRadius" > 2.75 </item>
     </style> 
</resources>

 然后在AndroidManifest.xml中使用刚才定义的主题。

只要定义application的android:theme属性为style/Theme即可。

<application
     android:allowBackup= "true"
     android:icon= "@drawable/ic_launcher"
     android:label= "@string/app_name"
     android:theme= "@style/Theme"  >
     <activity
         android:name= "com.example.themedemo.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>

 也可以用setTheme(R.style.Theme)来调用主题。效果图就不上传了。

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/14/2959164.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值