Android应用_Res_自定义控件XML自定义参数

本文详细介绍了如何在Android应用中自定义控件参数,包括定义参数、获取参数值及参数类型应用示例,覆盖从基本参数到复杂枚举、标志的全面解析。

自定义控件时,大家如何自定义参数?


<com.taptech.wemedia.view.TTTopicArticlesListView 
        xmlns:xlist="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tttalv_topic_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        xlist:xRefreshMode="both"/>

以xlist:XRefreshMode 为例

步骤一:

在res/values 下, 新建styles.xml 文件, 我定义了4种参数

 <resources>     
     <declare-styleable name="XListView">
	    <attr name="xRefreshMode">
	        <flag name="none" value="0x0" />
	        <flag name="pullToRefresh" value="0x01"/>
	        <flag name="loadMore" value="0x02"/>
	        <flag name="both" value="0x03"/>
	    </attr>
	</declare-styleable>
</resources>

步骤二:

在TTTopicArticlesListView中,获取XML中传入的参数

	// xlist:xRefreshMode
	private final int X_REFRESH_MODE_DEFAULT = -1;
	private final int X_REFRESH_MODE_NONE = 0;
	private final int X_REFRESH_MODE_PULL_TO_REFRESH = 1;
	private final int X_REFRESH_MODE_LOAD_MORE = 2;
	private final int X_REFRESH_MODE_BOTH = 3;
	public TTTopicArticlesListView(Context context) {
		super(context);
		//initWithContext(context);
		
		initWithAttrs(context, null);
	}

	public TTTopicArticlesListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//initWithContext(context);
		
		initWithAttrs(context, attrs);
		
	}

	public TTTopicArticlesListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		//initWithContext(context);
		
		initWithAttrs(context, attrs);
		
	}
	// 
	private void initWithAttrs(Context context, AttributeSet attrs) {
		
		
		// now style everything!
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.XListView);
		
// XListView_xRefreshMode 系统默认等价于以下内容
		//<declare-styleable name="XListView">
	    	//	<attr name="xRefreshMode">


int mode = ta.getInt(R.styleable.XListView_xRefreshMode, X_REFRESH_MODE_DEFAULT);

TTLog.e("XListView", mode + "");

switch (mode) {
case X_REFRESH_MODE_NONE:
setPullRefreshEnable(false);
setLoadMoreEnable(false);
break;

case X_REFRESH_MODE_PULL_TO_REFRESH:
setPullRefreshEnable(true);
setLoadMoreEnable(false);
break;

case X_REFRESH_MODE_LOAD_MORE:
setPullRefreshEnable(false);
setLoadMoreEnable(true);
break;

case X_REFRESH_MODE_BOTH:
setPullRefreshEnable(true);
setLoadMoreEnable(true);
break;


default:
setPullRefreshEnable(false);
setLoadMoreEnable(false);
break;
}


}


步骤三:

在使用TTTopicArticlesListView的地方

1. 声明xmlns, xlist 为自定义变量(可自定义),"http://schemas.android.com/apk/res-auto"(默认即可)

2. xRefreshMode属于自定义参数,为了能够访问得到,需要在前面加上xlist

<com.taptech.wemedia.view.TTTopicArticlesListView 
        xmlns:xlist="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tttalv_topic_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        xlist:xRefreshMode="both"/>


大功告成!

附(其它attrs 传入变量类型):

传入尺寸的,比如dip, dp, sp

<attr name="scrollOffset" format="dimension" />


传入颜色, 

<attr name="dividerColor" format="color" />        #FFFFFFFF

<attr name="tabBackground" format="reference" /> @android:color/transparent(顾名思义,任何@都可以

<attr name="tabBackground" format="color|reference" /> 二者结合

传入布尔值, false, true

<attr name="shouldExpand" format="boolean" />


引用某个控件,同上

<attr name="tabBackground" format="reference" />


传入float值

<attr name="fadeDegree" format="float" />


传入double值

<attr name="fadeDegree" format="double" />


Enum类型

       <attr name="touchModeBehind">
            <enum name="margin" value="0" />
            <enum name="fullscreen" value="1" />
        </attr>


Flag

<attr name="xRefreshMode">
       <flag name="none" value="0x0" />
       <flag name="pullToRefresh" value="0x01"/>
       <flag name="loadMore" value="0x02"/>
       <flag name="both" value="0x03"/>
   </attr>


还有没有别的,求补充~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值