// 房友宝的InfoBar
package com.fangyou.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class FangYouInfoBar extends RelativeLayout {
// <!-- Icon 文字 -->
private String iconText;
// <!-- 标题文字 -->
private String titleText;
// <!-- 标题文字颜色 -->
private int titleColor;
// <!-- 标题文字大小 -->
private float titleSize;
// <!-- 描述文字 -->
private String description;
// <!-- 描述文字颜色 -->
private int descriptionColor;
// <!-- 描述文字大小 -->
private float descriptionSize;
protected TextView mIcon; // Icon
protected TextView mTitle;// 标题
protected TextView mDescription; // 描述
public FangYouInfoBar(Context context, AttributeSet attrs) {
super(context, attrs);
initFromAttributes(context, attrs);
setUpView(context);
}
public FangYouInfoBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFromAttributes(context, attrs);
setUpView(context);
}
private void initFromAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FangYouInfoBar);
if (a != null) {
float defaultTitleSize = getResources().getDimension(R.dimen.sp10);
float defaultDescriptionSize = getResources().getDimension(R.dimen.sp6);
iconText = a.getText(R.styleable.FangYouInfoBar_iconText).toString();
titleText = a.getText(R.styleable.FangYouInfoBar_titleText).toString();
titleColor = a.getColor(R.styleable.FangYouInfoBar_titleColor, R.color.color_d0d0d0);
titleSize = a.getDimension(R.styleable.FangYouInfoBar_titleSize, defaultTitleSize);
description = a.getText(R.styleable.FangYouInfoBar_description).toString();
descriptionColor = a.getColor(R.styleable.FangYouInfoBar_descriptionColor, R.color.color_f5f5f5);
descriptionSize = a.getDimension(R.styleable.FangYouInfoBar_descriptionSize, defaultDescriptionSize);
a.recycle();
}
}
/**
* 初始化界面
*
* @param context
*/
private void setUpView(Context context) {
setGravity(Gravity.CENTER_VERTICAL);
int margins = (int) getResources().getDimension(R.dimen._10);
int width = (int) getResources().getDimension(R.dimen._46);
float defaultSize = getResources().getDimension(R.dimen.sp12);
int defaultColor = getResources().getColor(R.color.color_ffffff);
mIcon = new TextView(context);
mIcon.setId(0xff12);
mIcon.setGravity(Gravity.CENTER);
mIcon.setBackgroundResource(R.color.color_12c1c4);
mIcon.setText(iconText);
mIcon.setTextColor(defaultColor);
mIcon.setTextSize(defaultSize);
RelativeLayout.LayoutParams rlpm = new RelativeLayout.LayoutParams(width, width);
rlpm.addRule(RelativeLayout.CENTER_VERTICAL);
rlpm.setMargins(margins, 0, 0, 0);
addView(mIcon, rlpm);
mTitle = new TextView(context);
mTitle.setId(0xff17);
mTitle.setText(titleText);
mTitle.setTextColor(titleColor);
mTitle.setTextSize(titleSize);
rlpm = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlpm.addRule(RelativeLayout.RIGHT_OF, mIcon.getId());
rlpm.setMargins(margins, 0, 0, 0);
addView(mTitle, rlpm);
mDescription = new TextView(context);
mDescription.setText(description);
mDescription.setTextColor(descriptionColor);
mDescription.setTextSize(descriptionSize);
rlpm = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlpm.addRule(RelativeLayout.RIGHT_OF, mIcon.getId());
rlpm.addRule(RelativeLayout.BELOW, mTitle.getId());
rlpm.setMargins(margins, 4, 0, 0);
addView(mDescription, rlpm);
}
}
配置文件Attrs.xml
<declare-styleable name="FangYouInfoBar">
<!-- Icon 文字 -->
<attr name="iconText" format="string" />
<!-- 标题文字 -->
<attr name="titleText" format="string" />
<!-- 标题文字颜色 -->
<attr name="titleColor" format="color" />
<!-- 标题文字大小 -->
<attr name="titleSize" format="dimension" />
<!-- 描述文字 -->
<attr name="description" format="string" />
<!-- 描述文字颜色 -->
<attr name="descriptionColor" format="color" />
<!-- 描述文字大小 -->
<attr name="descriptionSize" format="dimension" />
</declare-styleable>
// demen.xml
<dimen name="sp10">10sp</dimen>
<dimen name="sp12">12sp</dimen>
<dimen name="sp6">6sp</dimen>