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,如需转载请自行联系原作者

目录
相关文章
|
9月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
225 1
|
Android开发
Android系统自带样式(android:theme)解析
做Android开发时经常会修改系统默认的主题样式,在android的sdk  安装目录data\res\values\themes.
2478 0
|
8月前
|
Android开发 开发者
Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。
【6月更文挑战第26天】Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。要更改主题,首先在该文件中创建新主题,如`MyAppTheme`,覆盖所需属性。然后,在`AndroidManifest.xml`中应用主题至应用或特定Activity。运行时切换主题可通过重新设置并重启Activity实现,或使用`setTheme`和`recreate()`方法。这允许开发者定制界面并与品牌指南匹配,或提供多主题选项。
133 6
|
8月前
|
Android开发 开发者
Android UI中的Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等。要更改主题
【6月更文挑战第25天】Android UI中的Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等。要更改主题,首先在`styles.xml`中定义新主题,如`MyAppTheme`,然后在`AndroidManifest.xml`中设置`android:theme`。可应用于全局或特定Activity。运行时切换主题需重置Activity,如通过`setTheme()`和`recreate()`方法。这允许开发者定制界面以匹配品牌或用户偏好。
86 2
|
API Android开发 UED
|
XML Android开发 数据格式
|
Android开发
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
618 0
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
|
API Android开发
Android中的Style、Theme详解已经发展史
版权声明:本文为sydMobile原创文章,转载请务必注明出处! https://blog.csdn.net/sydMobile/article/details/80164916 Style介绍 style就像单词意思一样,风格,这里面是属性的集合,如果页面中有许多控件的属性值相同那么就可以把这些属性抽出来放到style里面,定义也很简单,在values文件下的styles里面创建就可以了。
1691 0

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 2
    Android历史版本与APK文件结构
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 4
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 10
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 1
    android FragmentManager 删除所有Fragment 重建
    15
  • 2
    Android实战经验之Kotlin中快速实现MVI架构
    21
  • 3
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    24
  • 4
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    39
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 8
    Android历史版本与APK文件结构
    149
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40