如何在android style/layout文件中使用自定义属性。

自定义属性:

     <declare-styleable name="facepanelStyle">
        <!-- 底部Tab分割线背景 -->
        <attr name="tabSpiltColor" format="color" />
 </declare-styleable>


Style:引用自定义属性

 <style name="test">
        <item name="android:background">#ddd</item>
        <item name="tabSpiltColor">#f6f6f6</item>
    </style>

//此处并不需要引用报名,或者定义命名空间


layout:中引用

  <test.CustomView
                    style="@style/ftest"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                  
                    />


代码中得到属性值:

   1、 

public CustomView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public CustomView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  _context = context;
  getFacepanelStyle(attrs, defStyle);
  
 }



   private void getFacepanelStyle(AttributeSet attrs, int defStyle) {
  if (attrs != null) {
   TypedArray typedArray = _context.obtainStyledAttributes(attrs, R.styleable.facepanelStyle, defStyle, 0);

   int n = typedArray.getIndexCount();
   for (int i = 0; i < n; i++) {
    int attr = typedArray.getIndex(i);
    switch (attr) {

    case R.styleable.facepanelStyle_tabSpiltColor:
     tabBackground = typedArray.getColor(attr, Color.GRAY);

     break;
   

    }

   }
   typedArray.recycle();
  }
 }


>>>>>>>>>>>>>>>>>>>>>>>>>>>

如果在layout文件中直接使用

1, xmlns:expression=http://schemas.android.com/apk/包名“   //包名不好使时可换成res-auto

2,

 <test.CustomView
               
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    expression:tabSpiltColor="#f6f6f6"
                
                    />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值