自定义组合式控件

title_layout.xml:




    

在values文件夹下创建attrs文件:




    

        
        

        
        

    

新建一个MytitleView类:

package com.example.zuhe_demo1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by DELL on 2018/6/7.
 */

public class MytitleView extends LinearLayout implements View.OnClickListener{

    public MytitleView(Context context) {
        super(context);
    }

    public MytitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MytitleView, 0, 0);
        String title_text = typedArray.getString(R.styleable.MytitleView_title_text);
        int title_color = typedArray.getColor(R.styleable.MytitleView_title_color, Color.BLACK);

        String left_btn_text = typedArray.getString(R.styleable.MytitleView_left_btn_text);
        String right_btn_text = typedArray.getString(R.styleable.MytitleView_right_btn_text);

        View view = inflate(context, R.layout.title_layout, this);
        Button btn_left = view.findViewById(R.id.btn_left);
        btn_left.setOnClickListener(this);

        Button btn_right = view.findViewById(R.id.btn_right);
        btn_right.setOnClickListener(this);

        TextView tv_title = view.findViewById(R.id.tv_title);

        tv_title.setText(title_text);
        tv_title.setTextColor(title_color);

        btn_left.setText(left_btn_text);
        btn_right.setText(right_btn_text);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_left:
                if(onBtnClickLitener != null){
                    onBtnClickLitener.onLeftClick();
                }
                break;

            case R.id.btn_right:
                if(onBtnClickLitener != null){
                    onBtnClickLitener.onRightClick();
                }
                break;
        }
    }

    //定义一个接口
    public interface onBtnClickLitener{
        void onLeftClick();

        void onRightClick();
    }

    //实例化接口对象
    private onBtnClickLitener onBtnClickLitener;

    public void setOnBtnClickLitener(MytitleView.onBtnClickLitener onBtnClickLitener) {
        this.onBtnClickLitener = onBtnClickLitener;
    }
}

最后在activity_main.xml中引用:




    


最后的最后MainActivity:

package com.example.zuhe_demo1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private MytitleView titleview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        titleview = findViewById(R.id.titleview);

        titleview.setOnBtnClickLitener(new MytitleView.onBtnClickLitener() {
            @Override
            public void onLeftClick() {
                Toast.makeText(MainActivity.this, "返回", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightClick() {
                Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

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