public?class?TopBar?extends?RelativeLayout?{
private?Button?leftButton;//左邊按鈕
private?Button?rightButton;//右邊按鈕
private?TextView?tvTitle;???//中間標題
//聲明一些控件的屬性
private?int?leftTextColor;
private?Drawable?leftBackground;
private?String?leftText;
private?int?rightTextColor;
private?Drawable?rightBackground;
private?String?rightText;
private?int?titleTextColor;
private?String??title;
private?float?titleTextSize;
//為三個布局形式
private?LayoutParams?leftParams,rightParams,titleParams;
@SuppressLint("NewApi")?public?TopBar(Context?context,?AttributeSet?attrs)?{
super(context,?attrs);
Log.i("info",?"調用了自定義的屬性");
//拿到自己定義的屬性
TypedArray?ta=context.obtainStyledAttributes(R.styleable.TopBar);
leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,?0);
leftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
leftText=ta.getString(R.styleable.TopBar_leftText);
rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,?0);
rightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
rightText=ta.getString(R.styleable.TopBar_rightText);
titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor,?0);
title=ta.getString(R.styleable.TopBar_title);
titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,?15);
//回收掉??1、有可能浪費資源。2、避免由于一些直的緩存發生的錯誤
ta.recycle();
//創建控件
leftButton=new?Button(context);
rightButton=new?Button(context);
tvTitle=new?TextView(context);
//為三個空間設置屬性
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);//注意最小的sdk為16
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);//注意最小的sdk為16
rightButton.setText(rightText);
tvTitle.setText(title);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);//自己添加的屬性使其居中
//設置整體布局的背景顏色
setBackgroundColor(0x6699ff);
//為左部局參數
//第一個屬性為寬,第二個為高
leftParams=new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
//添加控件
addView(leftButton,leftParams);
//為右邊局參數
rightParams=new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT?,TRUE);
//添加控件
addView(rightButton,rightParams);
//為標題參數
titleParams=new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT?,TRUE);
//添加控件
addView(tvTitle,titleParams);
}
}
<?xml?version="1.0"?encoding="utf-8"?>
<resources>
?????<declare-styleable?name="TopBar">
?????????
?????????<attr?name="title"?format="string"/>
?????????<attr?name="titleTextSize"?format="dimension"/>
?????????<attr?name="titleTextColor"?format="color"/>
?????????<attr?name="leftTextColor"?format="color"/>
?????????<attr?name="leftBackground"?format="reference|color"/>
?????????<attr?name="leftText"?format="string"/>
?????????<attr?name="rightTextColor"?format="color"/>
?????????<attr?name="rightBackground"?format="reference|color"/>
?????????<attr?name="rightText"?format="string"/>
?????????
?????</declare-styleable>
????
</resources>
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????xmlns:tools="http://schemas.android.com/tools"
????android:layout_width="match_parent"
????android:layout_height="match_parent"?
????xmlns:app="http://schemas.android.com/apk/res/com.fdd.topbar">
????<com.fdd.topbar.TopBar
????????android:id="@+id/topbar"
????????android:layout_width="match_parent"
????????android:layout_height="50dp"
????????app:leftBackground="#000000"
????????app:leftText="左部按鈕"
????????app:leftTextColor="#ff0000"
????????app:rightBackground="#00000f"
????????app:rightText="右部按鈕"
????????app:rightTextColor="#ff00ff"
????????app:title="標題"
????????app:titleTextColor="#00ff00"
????????app:titleTextSize="25sp"?/>
</RelativeLayout>
2017-09-06
是改成auto ?老師說elipse不是用auto的 ? 可是事實是要用auto ?
2016-12-27
應該改成xmlns:app="http://schemas.android.com/apk/res-auto,我復制沒注意^_^
2016-12-27
在xml布局引用中,自定義命名空間app寫錯了,可以改為xmlns:android="http://schemas.android.com/apk/res-auto,同學記得多注意些重要細節哦!