房友宝的InfoBar

// 房友宝的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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值