除了布局,android中xml的其他用法
对于android新手来说,对xml的布局肯定是在熟悉不过。所以我们今天不说这个,我们来谈谈xml在android中除了布局,还有什么其他的用法?我直接总结一下有哪些其他用法:首先我们来看在drawable这个文件夹中有哪些用法。
要想看有哪些方法,我们可以借助提示工具,看有哪些标签。
1.selector这个对于初学者还是比较难以理解。selector就是状态列表(StateList) 它分为两种,一种Color-Selector 和Drawable-Selector。顾名思义一种是随着焦点变化颜色变化,一种是随着焦点变化图片变化
Button_style.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#ffffff" android:state_pressed="true" /><!-- pressed --> <item android:color="#000000" /> <!-- default --> <item android:state_focused="true" android:color="#cccccc"/> <!-- focused --></selector>
上段代码我们可以在button中引用
Button android:id="@+id/bt_about" style="@style/Button_style" android:background="@drawable/button_selector" android:layout_width="250dp" android:layout_height="50dp" android:layout_margin="5dp" android:textColor="@color/test_color_selector" android:text="@string/about" />
2.layer-list这个标签就把他理解为ps里面的图层就好,里面可以内嵌shape等标签,其中标签代表一个层
3.shape顾名思义就是可以用来形成图形的标签,如下代码,在android:shape里面有几个选项oval,line,rectangle等,其他有关的标签在代码里面提到
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- rectangle表示为矩形 --> <!-- 填充的颜色 --> <solid android:color="@color/blue" /> <!-- 边框的颜色和粗细 --> <stroke android:width="1dp" android:color="@color/blue" /> <!-- android:radius 圆角的半径 --> <corners android:radius="2dp" /></shape>
我们现在来分析一下以下代码,直接看代码,该代码实现的是只给图形加底边框,这种实现方式很重要,
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 连框颜色值 --> <item> <shape> <solid android:color="#ffffff" /> </shape> </item> <!-- 主体背景颜色值 ,这里是第二图层,将会覆盖前面的图层,如果我们让它bottom=15dp,底部的图层就将会露出15dp此时边框就出来了--> <item android:bottom="15dp"> <shape> <solid android:color="#000000" /> </shape> </item></layer-list>
下面我们来看一看xmll在values中attr.xml中的应用如下代码,主要用来自定义一些属性的
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="wheelview"> <attr name="gravity"> <enum name="center" value="17"/> <enum name="left" value="3"/> <enum name="right" value="5"/> </attr> <attr name="textSize" format="dimension"/> <attr name="textColorOut" format="color"/> <attr name="textColorCenter" format="color"/> <attr name="dividerColor" format="color"/> </declare-styleable> <declare-styleable name="TopBarView"> <attr name="fRightView" format="reference"></attr> <attr name="sRightView" format="reference"></attr> <attr name="leftView" format="reference"></attr> </declare-styleable></resources>
array.xml
我们通过这样的定义可以从资源里面获取数组,如何获取自行百度
<? xml version = "1.0" encoding = "UTF-8" ?>< resources >< string-array name = "citys" >< item > 北京 </ item >< item > 天津 </ item >< item > 上海 </ item >< item > 重庆 </ item ></ string-array ></ resources >
color.xml
这里用过来定义颜色的key value值,这个有时候非常有用,因为颜色代码容易忘,而对应的名字就好记多了
<?xml version="1.0" encoding="utf-8"?><resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#5c6ab8</color> <color name="colorAccent">#FF4081</color> <color name="colorPrimaryWeather">#5bdaff</color> <color name="colorPrimaryDarkWeather">#49aecc</color></resources>
dimens.xml
这个是用来定义具体的大小宽度的
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="nav_header_vertical_spacing">16dp</dimen> <dimen name="nav_header_height">160dp</dimen></resources>
string.xml
里面是用来定义常用字符串
<resources> <string name="app_name">Icalendar</string> <string name="navigation_drawer_open">Open navigation drawer</string> <string name="navigation_drawer_close">Close navigation drawer</string></resources>
最后就是比较重要的styles.xml了
这个可以自定义自己的主题,一般是通过继承andorid定义过的主题,然后重写其中的属性,用这个一般是因为android自带的主题并不能满足项目要求,所以要进行自定义,代码如下:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/calendar_bg</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="WeatherToolBarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimaryWeather</item> <item name="colorPrimaryDark">@color/colorPrimaryDarkWeather</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="OneToolBarTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimaryOne</item> <item name="colorPrimaryDark">@color/colorPrimaryDarkOne</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
在andorid代码这么去引用style="@style/xxx"
更多相关文章
- Eclipse与Android源码中ProGuard工具的使用(代码混淆)
- Anroid-vlc开源播放器代码编译及简单调用手把手
- Android 中的ListView选中项的背景颜色怎么设置?
- 浅谈android代码保护技术_ 加固
- Android之ActionBar、Tabs、Fragment、ViewPager实现标签页切换
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- 《第一行代码Android》学习总结第七章 运行时权限
- 用Go语言写Android应用 (2) - 从Android的Java调用Go代码
- Android 性能优化:使用 Lint 优化代码、去除多余资源