사용자 정의 컨트롤 만들기 복합 컨트롤
13248 단어 복합 공간
1. 사용자 정의 속성을 만드는 방법
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<!-- -->
<attr name="myTitle" format="string" />
<!-- dimension dp-->
<attr name="titleTextSize" format="dimension" />
<attr name="titleColor" format="color" />
<attr name="titleGravity" format="integer" />
<!-- -->
<attr name="leftText" format="string" />
<attr name="leftBackGround" format="color|reference" />
<attr name="leftTextColor" format="color" />
<!-- -->
<attr name="rightText" format="string" />
<attr name="rightBackground" format="color|reference" />
<attr name="rightTextColor" format="color" />
</declare-styleable>
</resources>
TypedArray typedArray =
context.obtainStyledAttributes(attrs, R.styleable.TopBar);
/** * , ViewGroup * */
private void initTopbar(Context context,AttributeSet attrs){
// xml
TypedArray typedArray =context.obtainStyledAttributes(attrs, R.styleable.TopBar);
titleName = typedArray.getString(R.styleable.TopBar_myTitle);
titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize,10);
titleColor = typedArray.getColor(R.styleable.TopBar_titleColor,0);
titleGravity = typedArray.getInteger(R.styleable.TopBar_titleGravity,10);
leftText = typedArray.getString(R.styleable.TopBar_leftText);
leftBackGround = typedArray.getDrawable(R.styleable.TopBar_leftBackGround);
leftTextColor = typedArray.getColor(R.styleable.TopBar_leftTextColor,0);
rightText = typedArray.getString(R.styleable.TopBar_rightText);
rightBackGround = typedArray.getDrawable(R.styleable.TopBar_rightBackground);
rightTextColor = typedArray.getInt(R.styleable.TopBar_rightTextColor,0);
typedArray.recycle();
Button leftBtn = new Button(context);
Button rightBtn = new Button(context);
TextView titleText = new TextView(context);
leftBtn.setText(leftText);
leftBtn.setBackground(leftBackGround);
leftBtn.setTextColor(leftTextColor);
LayoutParams leftParams =
new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftBtn,leftParams);
rightBtn.setText(rightText);
rightBtn.setBackground(rightBackGround);
rightBtn.setTextColor(rightTextColor);
LayoutParams rightParams =
new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightBtn,rightParams);
titleText.setText(titleName);
titleText.setTextSize(titleTextSize);
titleText.setTextColor(titleColor);
titleText.setGravity(titleGravity);
LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(titleText,titleParams);
addListener(leftBtn,rightBtn);
}
/** * * @param leftBtn button * @param rightBtn button * */
private void addListener(Button leftBtn,Button rightBtn) {
leftBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.leftClick();
}
});
rightBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.rightClick();
}
});
}
/** * * 。 * */
public void setOntopbarClickListener(TopbarClickListener mListener){
this.mListener = mListener;
}
public interface TopbarClickListener{
void leftClick();
void rightClick();
}
<?xml version="1.0" encoding="utf-8"?>
<com.xiaoxu.administrator.myserviceapp.view.MyTopbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:topbar ="http://schemas.android.com/apk/res-auto"
android:id="@+id/topBar"
android:layout_width="wrap_content"
android:layout_height="50dp"
topbar:myTitle =" "
topbar:titleTextSize="10dp"
topbar:titleColor ="#123412"
topbar:titleGravity ="20"
topbar:leftText=" "
topbar:leftBackGround="@drawable/left_pic"
topbar:leftTextColor ="#FFFFFF"
topbar:rightText=" "
topbar:rightBackground="@drawable/right_pic"
topbar:rightTextColor="#FFFFFF">
</com.xiaoxu.administrator.myserviceapp.view.MyTopbar>
- 그런 다음 활동의 레이아웃에서 다음을 참조할 수 있습니다. - 설정된 속성을 현재 활동에 설정합니다.
<include layout="@layout/topbar_layout"/>
MyTopbar myTopbar = (MyTopbar) findViewById(R.id.topBar);
myTopbar.setOntopbarClickListener(new MyTopbar.TopbarClickListener() {
@Override
public void leftClick() {
//
Toast.makeText(SecondActivity.this," LeftClick ",Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
//
Toast.makeText(SecondActivity.this," RightClick ",Toast.LENGTH_SHORT).show();
}
});
효과는 그림과 같아서 보기에는 매우 low적이다. 좋은 그림이 없어서 마음대로 두 장을 만들었는데 그림이 있으면 다시 고쳐도 소용없다.하하