Android中自定义attr,以及style杂谈

attr 属性 
style 样式 
二者都是在res/values下面的xml文件 

attr: for example: 

Java代码   收藏代码
  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  


类似的"layout_height,layout_width"都是属性 

style就是这个控件设定好的一些值,方便重复调用 
自定义的一个样式"TextStyle" 
使用的android默认的属性shadowDx..设置好的具体的值 
Java代码   收藏代码
  1. <style name="TextStyle">  
  2.   <item name="android:shadowDx">-0.5</item>  
  3.   <item name="android:shadowDy">1</item>  
  4.   <item name="android:shadowRadius">0.5</item>  
  5.   <item name="android:singleLine">true</item>  
  6.   <item name="android:ellipsize">marquee</item>  
  7. </style>  


自定义的attr: 这个format就是格式 
reference   表示引用,参考某一资源ID 
string   表示字符串 
color   表示颜色值 
dimension   表示尺寸值 
boolean   表示布尔值 
integer   表示整型值 
float   表示浮点值 
fraction   表示百分数 
enum   表示枚举值 
flag   表示位运算 

Java代码   收藏代码
  1. <resources>  
  2.     <declare-styleable name="ViewPagerIndicator">  
  3.         <!-- Style of the circle indicator. -->  
  4.         <attr name="vpiCirclePageIndicatorStyle" format="reference"/>  
  5.         <!-- Style of the icon indicator's views. -->  
  6.         <attr name="vpiIconPageIndicatorStyle" format="reference"/>  
  7.         <!-- Style of the line indicator. -->  
  8.         <attr name="vpiLinePageIndicatorStyle" format="reference"/>  
  9.         <!-- Style of the title indicator. -->  
  10.         <attr name="vpiTitlePageIndicatorStyle" format="reference"/>  
  11.         <!-- Style of the tab indicator's tabs. -->  
  12.         <attr name="vpiTabPageIndicatorStyle" format="reference"/>  
  13.         <!-- Style of the underline indicator. -->  
  14.         <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>  
  15.     </declare-styleable>  
  16. </resources>  


使用的时候,在布局文件头里 
添加 
xmlns: myapp="http://schemas.android.com/apk/res/包名" 

在xml文件里使用跟系统自带控件一样使用 
类似下面的语法 
myapp:vpiIconPageIndicatorStyle="xxxxx" 

====================================================================================================================================================================
上边的如果要使用自定义的属性进行自定义样式 
格式应该是 
Java代码   收藏代码
  1.     <style name="Theme.PageIndicatorDefaults" parent="android:Theme">  
  2.         <item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item>  
  3.         <item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item>  
  4.     </style>  
  5.   
  6. //上图样式的名称是"Theme.PageIndicatorDefaults",包含两个属  
  7. //性"vpiIconPageIndicatorStyle"和"vpiTabPageIndicatorStyle",值都是引用类型的  


attr和style格式都是需要<resources>起头的 

获取示例: 

Java代码   收藏代码
  1. TypedArray a = context.obtainStyledAttributes(null, R.styleable.ViewPagerIndicator,00);  
  2. int rsid= a.getResourceId(R.styleable.ViewPagerIndicator_vpiTabPageIndicatorStyle, 0);  
  3. a.recycle();//必须回收  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值