自定义组合式控件

title_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/btn_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:gravity="center"
        />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        />
</LinearLayout>

在values文件夹下创建attrs——attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MytitleView">

        <attr name="title_text" format="string"></attr>
        <attr name="title_color" format="color"></attr>

        <attr name="left_btn_text" format="string"></attr>
        <attr name="right_btn_text" format="string"></attr>

    </declare-styleable>

</resources>

创建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中引用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zuhe_demo1.MainActivity">

    <com.example.zuhe_demo1.MytitleView
        android:id="@+id/titleview"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        app:title_text="标题"
        app:title_color="@color/colorAccent"
        app:left_btn_text="返回"
        app:right_btn_text="搜索"
        ></com.example.zuhe_demo1.MytitleView>

</RelativeLayout>

最后的最后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();
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/gaoyiranblog/article/details/80801741
今日推荐