package com.sb.testzdyview;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.ViewGroup;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;? @SuppressLint("NewApi") public class Topbar extends RelativeLayout { private Button leftButton,rightButton; private TextView tvTitle; private LayoutParams leftParam,rightParam,titleParam; private int leftTextColor; private Drawable leftBackgroud; private String leftText; private int rightTextColor; private Drawable rightBackgroud; private String rightText; private float titleTextSize; private int titleTextColor; private String title;? ? ?@SuppressLint("NewApi") public Topbar(Context context, AttributeSet attrs) { super(context, attrs); // 得到atts中的自定義屬相并賦給ta集合 TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.Topbar); ? ?//相當(dāng)于初始化atts中的屬性,在下面的方法中有兩個(gè)默認(rèn)的參數(shù)常用到,index,defvalue:默認(rèn)值 leftTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); leftText=ta.getString(R.styleable.Topbar_leftText); rightTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); rightBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); rightText=ta.getString(R.styleable.Topbar_rightText); titleTextSize=ta.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor=ta.getColor(R.styleable.Topbar_titleTextColor, 0); title=ta.getString(R.styleable.Topbar_title); ta.recycle();//回收避免浪費(fèi)資源,清除緩存 //初始化用到的三個(gè)控件 leftButton=new Button(context); rightButton=new Button(context); tvTitle=new TextView(context); //把用到的控件和新定義的屬性關(guān)聯(lián)在一起 leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackgroud); leftButton.setText(leftText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackgroud); rightButton.setText(rightText); tvTitle.setTextSize(titleTextSize); tvTitle.setTextColor(titleTextColor); tvTitle.setText(title); //設(shè)置tvTitlt居中 tvTitle.setGravity(Gravity.CENTER); //給viewgroup添加背景色 setBackgroundColor(0xfff59563); //定義leftParam的狂傲屬性 leftParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //給leftParam增加一個(gè)規(guī)則,在viewGroup中居左對(duì)齊 leftParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); //把leftButton加入到leftParam中 addView(leftButton,leftParam); rightParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //給leftParam增加一個(gè)規(guī)則,在viewGroup中居左對(duì)齊 rightParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); //把leftButton加入到leftParam中 addView(rightButton,rightParam); titleParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); titleParam.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(tvTitle,titleParam); }}
自定義view?
負(fù)手_睥睨天下
2016-08-11 17:46:20