Android 自定义组合控件

这里是自定义标题栏控件
1、首先建立一个resource文件最为控件属性配置
atts.xml



    
        
        
        
        
        
        
        
        
         
    


2、然后继承RelativeLayout类,实现组合自定义控件和方法,添加自定义回调接口
Topbar.java

package com.example.mydemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Topbar extends RelativeLayout {

    private Button leftButton, rightButton;
    private TextView title;

    private String titleText;
    private float titleTextSize;
    private int titleTextColor;

    private String leftButtonText;
    private Drawable leftBackground;
    private int leftTextColor;

    private String rightButtonText;
    private Drawable rightBackground;
    private int rightTextColor;

    private LayoutParams leftParams, rightParams, titleParams;

    private OnTopBarClickListener listener;

    interface OnTopBarClickListener {
        public void leftClick();

        public void rightClick();
    }

    void setOnTopBarClick(OnTopBarClickListener listener) {
        this.listener = listener;
    }

    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.Topbar);
        titleText = ta.getString(R.styleable.Topbar_title);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);

        leftButtonText = ta.getString(R.styleable.Topbar_leftText);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);

        rightButtonText = ta.getString(R.styleable.Topbar_rightText);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);

        //setBackgroundColor(0xFFF59563);
        // 回收资源
        ta.recycle();

        title = new TextView(context);
        leftButton = new Button(context);
        rightButton = new Button(context);
        // 属性设置
        title.setText(titleText);
        title.setTextColor(titleTextColor);
        title.setTextSize(titleTextSize);

        leftButton.setText(leftButtonText);
        leftButton.setBackground(leftBackground);
        leftButton.setTextColor(leftTextColor);

        rightButton.setText(rightButtonText);
        rightButton.setBackground(rightBackground);
        rightButton.setTextColor(rightTextColor);

        // 宽高设置
        leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        // 位置规则设置
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftButton, leftParams);

        rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightButton, rightParams);

        titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        titleParams.setMargins(0, 20, 0, 0);
        addView(title, titleParams);

        //接口回调
        leftButton.setOnClickListener(new OnClickListener() {       
            @Override
            public void onClick(View arg0) {
                //让调用者去实现我们自己定义的接口
                listener.leftClick();
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                listener.rightClick();
                
            }
        });
    }

}

然后再我们需要的布局上引入我们的自定义控件,像这样
xmlns:app="http://schemas.android.com/apk/res/com.example.mydemo"
就可以用了 。
rela.xml



    
    
      
        
        
    



MainActivity.java

package com.example.mydemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.mydemo.Topbar.OnTopBarClickListener;

public class MainActivity extends Activity {

    private Topbar tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rela);
        tb = (Topbar) findViewById(R.id.topbar);
        tb.setOnTopBarClick(new OnTopBarClickListener() {
            
            @Override
            public void rightClick() {
                Toast.makeText(MainActivity.this, "前进", Toast.LENGTH_SHORT).show();
                
            }
            
            @Override
            public void leftClick() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "后退", Toast.LENGTH_SHORT).show();
            }
        });
    }


    

}

演示效果:


Android 自定义组合控件_第1张图片
topbar.gif

你可能感兴趣的:(Android 自定义组合控件)