在学习开发过程中有许多时候需要使用自定义控件。。然而如何在布局过程中更加方便的定义自定义的属性呢?
就拿上一篇博客:仿微信6.0底部菜单选择和滑动效果中自定义底部菜单中的控件来说。。在控件中
我们需要规定渐变颜色和字体大小等属性,为了像android:textsize="14sp"这样方便的开发。。我们可以在项目文件夹下的res\values
文件夹中创建attrs.xml
如同:
- <?xml version="1.0" encoding="utf-8" ?>
- - <resources>
- - <declare-styleable name="MyTabExchangeView">
- <attr name="textColor" format="color" />
- <attr name="textSize" format="dimension" />
- <attr name="textString" format="string" />
- <attr name="iconColor" format="color" />
- <attr name="iconDrawable" format="reference|color" />
- </declare-styleable>
- </resources>
formate属性值说明:
1. reference:资源id2. color:颜色值
3. boolean:布尔值
4. dimension:尺寸值
5. float:浮点值
6. integer:整型值
7. string:字符串
8. fraction:百分数
9. enum:枚举值
10. flag:位或运算
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:ww="http://schemas.android.com/apk/res/com.ww.mybottomtitle"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <android.support.v4.view.ViewPager
- android:id="@+id/fragment_pager"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_above="@+id/bottom_title"
- />
- <LinearLayout
- android:id="@+id/bottom_title"
- android:layout_width="fill_parent"
- android:layout_height="48dp"
- android:layout_alignParentBottom="true"
- android:orientation="horizontal"
- android:background="#F0F0F0"
- >
- <com.ww.mybottomtitle.MyTabExchangeView
- android:id="@+id/index"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- ww:textString="首页"
- android:paddingTop="5dp"
- android:paddingBottom="5dp"
- ww:iconDrawable="@drawable/icon_home"
- ww:textSize="10sp"
- />
- <com.ww.mybottomtitle.MyTabExchangeView
- android:id="@+id/news"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:paddingTop="5dp"
- android:paddingBottom="5dp"
- ww:textString="咨询"
- ww:iconDrawable="@drawable/icon_dingyue"
- ww:textSize="10sp"
- />
- <com.ww.mybottomtitle.MyTabExchangeView
- android:id="@+id/happy"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:paddingTop="5dp"
- android:paddingBottom="5dp"
- ww:textString="娱乐"
- ww:iconDrawable="@drawable/icon_list"
- ww:textSize="10sp"
- />
- <com.ww.mybottomtitle.MyTabExchangeView
- android:id="@+id/myself"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:paddingTop="5dp"
- android:paddingBottom="5dp"
- ww:textString="我"
- ww:iconDrawable="@drawable/icon_health_data"
- ww:textSize="10sp"
- />
- </LinearLayout>
- </RelativeLayout><strong>
- </strong>
xmlns:ww="http://schemas.android.com/apk/res/com.ww.mybottomtitle"其中ww是可以自己定义,但是后面的包名com.ww.mybottomtitle必须是整个项目的包名,我在最开始使用的时候就是犯了这个错误误以为是自定义控件所在的包名,最终错误。。。。你们懂的。。。debug调试调的差点崩溃。。
如下:
这样我们就可以得到我们从xml中所写入的数据啦。。是不是很方便呢?当然为了保持以后使用该属性的一致性我们需要进行array.recycle() 了。
- TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyTabExchangeView);
- for (int i = 0; i < array.getIndexCount(); i++) {
- int type=array.getIndex(i);
- switch (type) {
- case R.styleable.MyTabExchangeView_iconColor:
- iconColor=array.getColor(type,0xff45c01a);
- break;
- case R.styleable.MyTabExchangeView_iconDrawable:
- Drawable iconBitmapDrawable = array.getDrawable(type);
- iconBitmap=((BitmapDrawable)iconBitmapDrawable).getBitmap();
- break;
- case R.styleable.MyTabExchangeView_textColor:
- textColor=array.getColor(type,0xff45c01a);
- break;
- case R.styleable.MyTabExchangeView_textSize:
- textSize=array.getDimension(type,10);
- break;
- case R.styleable.MyTabExchangeView_textString:
- textString =array.getString(type);
- break;
- default:
- break;
- }
- }
- array.recycle();