public?class?MyView?extends?RelativeLayout?{
????private?Button?rightBtn,LeftBtn;
????private?TextView?tvTitle;
????private?String?TitleText;
????private?float?TitleSize;
????private?int?TitleColor;
????private?String?LeftText;
????private?Drawable?leftBackground;
????private?int?LeftColor;
????private?String?rightText;
????private?Drawable?rightBackground;
????private?int?rightColor;
????
????private?LayoutParams?leftParams,rightParams,TitleParmas;
????public?MyView(Context?context,?AttributeSet?attrs)?{
????????super(context,?attrs);
????????TypedArray?ta?=?context.obtainStyledAttributes(attrs,R.styleable.TopBar);
????????LeftText??=?ta.getString(R.styleable.TopBar_LeftTitle);
????????leftBackground?=?ta.getDrawable(R.styleable.TopBar_LeftTitleBackground);
????????LeftColor?=?ta.getColor(R.styleable.TopBar_LeftTitleTextColor,0);
????????rightText??=?ta.getString(R.styleable.TopBar_RightTitle);
????????rightBackground?=?ta.getDrawable(R.styleable.TopBar_RightTitleBackground);
????????rightColor?=?ta.getColor(R.styleable.TopBar_RightTitleTextColor,0);
????????TitleText??=?ta.getString(R.styleable.TopBar_Title);
????????TitleSize?=?ta.getDimension(R.styleable.TopBar_TitleTextSize,0);
????????TitleColor?=?ta.getColor(R.styleable.TopBar_TitleTextColor,0);
????????ta.recycle();
????????LeftBtn?=?new?Button(context);
????????rightBtn?=?new?Button(context);
????????tvTitle?=?new?TextView(context);
????????LeftBtn.setText(LeftText);
????????LeftBtn.setBackground(leftBackground);
????????LeftBtn.setTextColor(LeftColor);
????????rightBtn.setText(rightText);
????????rightBtn.setBackground(rightBackground);
????????rightBtn.setTextColor(rightColor);
????????tvTitle.setText(TitleText);
????????tvTitle.setTextSize(TitleSize);
????????tvTitle.setTextColor(TitleColor);
????????tvTitle.setGravity(Gravity.CENTER);
????????setBackgroundColor(0xFFF59563);
????????
????????leftParams?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.WRAP_CONTENT);
????????leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
????????
????????addView(LeftBtn,leftParams);
????????rightParams?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.WRAP_CONTENT);
????????rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
????????addView(rightBtn,rightParams);
????????TitleParmas?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.MATCH_PARENT);
????????TitleParmas.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
????????addView(tvTitle,TitleParmas);
????}


2016-11-03
如果出現(xiàn)命名空間找不到的問(wèn)題,一定要注意,declare 中聲明的name 一定要和建立的Java文件的名字一模一樣!
2016-08-02
@西瓜牛奶你的問(wèn)題解決沒(méi),我也遇到一樣的問(wèn)題了
2016-07-02
把自定義的屬性在xml中都寫全就顯示出來(lái)了。還有要注意當(dāng)前model中的自定義屬性是否同其他定義的屬性重名(如果重名,會(huì)R報(bào)錯(cuò))。