ZNDS智能电视网 推荐当贝市场

TV应用下载 / 资源分享区

软件下载 | 游戏 | 讨论 | 电视计算器

综合交流 / 评测 / 活动区

交流区 | 测硬件 | 网站活动 | Z币中心

新手入门 / 进阶 / 社区互助

新手 | 你问我答 | 免费刷机救砖 | ROM固件

查看: 14631|回复: 0
上一主题 下一主题
[教程]

第三十二讲:Android中的主题和风格学习指南

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-28 16:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
>  
     
  Open Home 还带有抽屉效果^_^好吧,我承认这个界面也谈不上好看和耐看,不过我们学了本讲的内容就可以按照自己的意愿更改程序的外观显示了。别人做的不好,咱自己做还不行吗(心虚中,^_^)……二、什么是主题(Theme)和风格(Style):
   
  在Web开发中,Html代码负责内容部分,CSS部分负责表现部分,我们使用CSS+DIV的符合Web标准的方式设计网页,可以把内容和形式分离开。同样在Android的开发中,我们可以使用Theme、Style+UI组件的方式,实现内容和形式的分离,做到界面的自定义。那么我们下面近距离认识一下Theme和Style。
   
  风格Style是一个包含一种或多种格式化属性的集合,你可以把它应用在UI组件上。主题Theme也是一个包含一种或多种格式化属性的集合,你可以把它应用在整个应用程序(Application)中或者某个窗口(Activity)中。
   
  定义一个style或者theme的方法是一样的。在res/values/目录下建立style.xml或者theme.xml文件,在xml中建立形如这样的代码:(注,这段代码来自官方的文档,不是我写的…)
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <resources>   
    <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>   
        </style>   
       
    </resources>
复制代码
  系统提供了大量的Style和Theme使我们学习样式设计的最好教材,我把代码放在这里(styles.xml)和这里(themes.xml)了,有时间大家可以多看看系统的样式是如何定义的。   
  三、自定义主题(Theme)和风格(Style):   
   
  下面让我们借用http://blog.androgames.net/category/android-tutorials/page/5/ 里的例子加工一下来学习如何自定义UI组件的风格   
吧,哎这个例子实在是有点小酷,所以你先看一下效果好了。   
   
   
     
1、 新建一个项目 Lesson32_StyleAndTheme2、拷贝这         三张 Nine-Patch PNG图片到res/drawable目录下,对于什么是nine-patch图片,以及它是如何制作的,感兴趣的朋友可以看这里,我们这里只需要知道它可以不失真的情况下进行缩放伸缩就可以了。3、在按钮的同目录下建立一个文件btn_custom.xml,把上述3张图片整合成一个按钮背景文件,让三张图片成为不同状态下的按钮表现效果。具体写法如下:   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
            <item android:drawable="@drawable/btn_red" android:state_enabled="false">   
            <item android:drawable="@drawable/btn_orange" android:state_enabled="true" android:state_pressed="true">   
            <item android:drawable="@drawable/btn_orange" android:state_enabled="true" android:state_focused="true">   
            <item android:drawable="@drawable/btn_black" android:state_enabled="true">   
    </item></item></item></item></selector>
复制代码
  4、在res/values目录下定义style.xml文件,内容如下:   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <resources>   
    <style name="BasicButtonStyle" parent="@android:style/Widget.Button">   
                    <item name="android:gravity">center_vertical|center_horizontal</item>   
                    <item name="android:textColor">#ffffffff</item>   
                    <item name="android:shadowColor">#ff000000</item>   
                    <item name="android:shadowDx">0</item>   
                    <item name="android:shadowDy">-1</item>   
                    <item name="android:shadowRadius">0.2</item>   
                    <item name="android:textSize">16dip</item>   
                    <item name="android:textStyle">bold</item>   
                    <item name="android:background">@drawable/btn_custom</item>   
                    <item name="android:focusable">true</item>   
                    <item name="android:clickable">true</item>   
            </style>   
    <style name="BigTextStyle">   
        <item name="android:layout_margin">5dp</item>   
                    <item name="android:textColor">#ff9900</item>   
                    <item name="android:textSize">25sp</item>   
      </style>   
    <style name="BasicButtonStyle.BigTextStyle">   
        <item name="android:textSize">25sp</item>   
      </style>   
       
    </resources>
复制代码
    5、在res/layout/目录下定义main.xml文件,内容如下:   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">   
       
            <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="张信哲的热搜歌曲">   
       
            <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="爱如潮水 " android:id="@+id/Button01">   
      </button>   
       
      <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="过火 " android:id="@+id/Button02">   
      </button>   
       
      <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="别怕我伤心 " android:id="@+id/Button03">   
      </button>   
       
      <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="从开始到现在 " android:id="@+id/Button04">   
      </button>   
       
      <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="信仰 " android:id="@+id/Button05">   
      </button>   
       
      <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="说谎 " android:id="@+id/Button06">   
      </button>   
    </textview></linearlayout>
复制代码
  6、在res/values目录下定义theme.xml文件:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <resources>           
    <style name="BasicButtonTheme">   
                    <item name="android:buttonStyle">@style/basicbuttonstyle</item>   
                    <item name="android:windowBackground">@android:color/transparent</item>     
                    <item name="android:windowIsTranslucent">true</item>     
      </style>   
       
    </resources>
复制代码
    7、在AndroidManifest.xml中给整个应用程序设置主题:
   
   
  我们是不是清晰的看到主题在应用程序层定义后,它的子元素会自动继承,这一点非常像CSS。说到继承,我在这个例子的代码里也放了一些关于样式可以继承的使用指引,相信有细心的朋友已经注意到了。   
   
  最后强调再强调一遍哈,代码一定要自己敲一遍才会有更大收获,拷贝代码则会让你很快就忘记所学内容。   
   
  好了本讲就到这里,Take your time and enjoy it ^_^   
   
   
   

上一篇:第四十五讲:用户界面 View(十二)
下一篇:android读取excel的数据
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

Archiver|新帖|标签|软件|Sitemap|ZNDS智能电视网 ( 苏ICP备2023012627号 )

网络信息服务信用承诺书 | 增值电信业务经营许可证:苏B2-20221768 丨 苏公网安备 32011402011373号

GMT+8, 2025-8-16 15:29 , Processed in 0.064920 second(s), 15 queries , Redis On.

Powered by Discuz!

监督举报:report#znds.com (请将#替换为@)

© 2007-2025 ZNDS.Com

快速回复 返回顶部 返回列表