Android 自定义UI控件模板 组合模式:
一.自定义属性:
在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);
}
}
三.布局中使用
<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>
Android: 自定义View
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/1129/6820.html