res/values目录常用xml文件

提示:获取Resources 

Java代码   收藏代码
  1. Resources Resources = getBaseContext().getResources();  
 

一、strings.xml代码 


Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <resources>     
  3.     <string name="hello">Hello World, Main!</string>     
  4. </resources>  


获取代码: 

Java代码   收藏代码
  1. String app_name = getResources().getString(R.string.app_name);  
 


二、arrays.xml代码 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     <array    
  4.         name="items">    
  5.         <item>中国</item>    
  6.         <item>日本</item>    
  7.         <item>韩国</item>    
  8.     </array>    
  9.     <integer-array    
  10.         name="moneys">    
  11.         <item>100</item>    
  12.         <item>200</item>    
  13.         <item>300</item>    
  14.         <item>400</item>    
  15.         <item>500</item>    
  16.     </integer-array>    
  17.     <string-array    
  18.         name="levels">    
  19.         <item>一等级</item>    
  20.         <item>二等级</item>    
  21.         <item>三等级</item>    
  22.     </string-array>    
  23. </resources>  
 


获取代码: 

Java代码   收藏代码
  1. String items[] = getResources().getStringArray(R.array.level);     
  2. int int_array[] = getResources().getIntArray(R.array.moneys);  
 



三、colors.xml代码 


Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <resources>     
  3.     <color name="white">#ffffff</color>     
  4.     <color name="black">#000000</color>     
  5.     <color name="grey">#333333</color>     
  6.     <color name="yellow">#fccf29</color>     
  7. </resources>  
 


获取代码: 

Java代码   收藏代码
  1. int color = getResources().getColor(R.color.yellow);     
  2. textView1.setTextColor(color);     
  3. //或者写成textView1.setTextColor(Color.YELLOW);  
 



四、styles.xml代码 


Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <resources>     
  3.     <style name="titleText">     
  4.         <item name="android:textSize">18px</item>     
  5.         <item name="android:textColor">#213255</item>     
  6.         <item name="android:background">#888888</item>     
  7.     </style>     
  8. </resources>  
 


获取代码(xml): 

Xml代码   收藏代码
  1. style="@style/titleText"  
 



五、dimens.xml代码 


Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <resources>     
  3.     <dimen name="big_font">30px</dimen>     
  4.     <dimen name="middle_font">20px</dimen>     
  5.     <dimen name="small_font">10px</dimen>     
  6. </resources>  
 


获取代码: 

Java代码   收藏代码
  1. float dimens = getResources().getDimension(R.dimen.big_font);     
  2. xml_dimens.setTextSize(dimens);  
 
在Android应用程序中,`app/src/main/res/values/styles.xml` 文件是一个关键的地方,用于定义应用的主题和样式的全局设置。如果你想添加一个新的"连连看公共样式",你可以按照以下步骤操作: 1. 打开`styles.xml` 文件,这个文件通常包含一系列预定义的样式主题,如 `Theme.AppCompat.Light.NoActionBar`、`Widget.MaterialComponents.Button.TextButton` 等。 2. 在文件的 `<resources>` 标签内,找到或创建一个 `<style>` 元素,例如,你可以新建一个样式并命名为 `@style/LianLianGameTheme`: ```xml <style name="LianLianGameTheme" parent="Theme.AppCompat.Light"> <!-- 添加连连看特有的样式属性 --> <item name="colorPrimary">@color/ll_game_primary_color</item> <item name="colorPrimaryDark">@color/ll_game_primary_dark_color</item> <item name="colorAccent">@color/ll_game_accent_color</item> <item name="android:textColorPrimary">@color/ll_game_text_color</item> <!-- ...其他你需要的样式属性... --> </style> ``` 这里我们设置了几个基本的颜色属性,比如主色、深色背景色、强调色以及文本颜色。具体的属性值需要替换为你项目中实际的颜色资源引用。 3. 如果你想让这个新的样式在整个应用中生效,可以将它设置为默认的主题,通过 `<application>` 标签内的 `<theme>` 属性: ```xml <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/LianLianGameTheme"> <!-- ... --> </application> ``` 现在,你已经成功地在 `styles.xml` 中添加了 "连连看公共样式"。开发者可以根据这个主题定制应用界面的整体风格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值