Android 自定義UI控件模板 組合模式:html
一.自定義屬性:android
在res/values中建立attr.xml文件,並添加如下代碼(若有則直接添加):數據結構
<?xml version="1.0" encoding="utf-8"?> <resources> <!--自定義屬性--> <attr name="leftTitleText" format="string" /> <attr name="leftTitleTextColor" format="color" /> <attr name="leftTitleTextSize" format="dimension" /> <attr name="tText" format="string" /> <attr name="tTextColor" format="color" /> <attr name="tTextSize" format="dimension" /> <declare-styleable name="AboutView"> <attr name="leftTitleText" /> <attr name="leftTitleTextColor" /> <attr name="leftTitleTextSize" /> <attr name="leftTitleBackground" format="color|reference" /> <attr name="tText" /> <attr name="tTextColor" /> <attr name="tTextSize" /> <attr name="tBackground" format="color|reference" /> </declare-styleable> <!--自定義屬性--> </resources>
二.自定義控件:佈局
package com.uwo.ui.swipelv.view; import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.AttributeSet; import android.view.Gravity; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.uwo.ui.swipelv.R; /** * Created by SRain on 2016/3/15. * * 自定義控件模板--關於中LinearLayout中添加兩個TextView */ public class AboutView extends LinearLayout { private TextView leftTv, titleTv; private String leftText; private int leftTvColor; private float leftTvTextSize; private Drawable leftTvBg; private String titleText; private int titleTvColor; private float titleTvTextSize; private Drawable titleTvBg; private LayoutParams leftParams; private LayoutParams titleParams; public AboutView(Context context) { super(context); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public AboutView(Context context, AttributeSet attrs) { super(context, attrs); /** * TypeArray 數據結構,獲取xml中自定義屬性值 * 使用時相似於Map,鍵值對模式 * leftText = typedArray.getString(R.styleable.AboutView_leftTitleText); * AboutView_leftTitleText 是AboutView自定義屬性中leftTitleText的值 */ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AboutView); leftText = typedArray.getString(R.styleable.AboutView_leftTitleText); leftTvColor = typedArray.getColor(R.styleable.AboutView_leftTitleTextColor, 0); leftTvTextSize = typedArray.getDimension(R.styleable.AboutView_leftTitleTextSize, 0); leftTvBg = typedArray.getDrawable(R.styleable.AboutView_leftTitleBackground); titleText = typedArray.getString(R.styleable.AboutView_tText); titleTvColor = typedArray.getColor(R.styleable.AboutView_tTextColor, 0); titleTvTextSize = typedArray.getDimension(R.styleable.AboutView_tTextSize, 0); titleTvBg = typedArray.getDrawable(R.styleable.AboutView_tBackground); typedArray.recycle(); leftTv = new TextView(context); titleTv = new TextView(context); leftTv.setText(leftText); leftTv.setTextColor(leftTvColor); leftTv.setTextSize(leftTvTextSize); leftTv.setBackground(leftTvBg); titleTv.setText(titleText); titleTv.setTextColor(titleTvColor); titleTv.setTextSize(titleTvTextSize); titleTv.setBackground(titleTvBg); leftParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); leftParams.gravity = Gravity.CENTER; leftParams.weight = 3; leftTv.setGravity(Gravity.CENTER); addView(leftTv, leftParams); titleParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); titleParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL; titleParams.weight = 2; titleTv.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); addView(titleTv, titleParams); } }
三.佈局中使用ui
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:about="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <!--xmlns:about=" 引用本身的屬性 --> <com.uwo.ui.swipelv.view.AboutView android:orientation="horizontal" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="50dp" android:paddingLeft="10dp" android:paddingRight="10dp" about:leftTitleBackground="#de000000" about:leftTitleText="版本更新" about:leftTitleTextColor="#fff5f5f5" about:leftTitleTextSize="10sp" about:tBackground="#de000000" about:tTextSize="10sp" about:tText="v 1.0.0" about:tTextColor="#fff5f5f5" /> </RelativeLayout>
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/1129/6820.htmlspa