自定义View(标题)

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 array = context.obtainStyledAttributes(attrs, R.styleable.MyTiltleView);
        String title_text = array.getString(R.styleable.MyTiltleView_title_text);
        String btn_left = array.getString(R.styleable.MyTiltleView_btn_left_text);
        String btn_right = array.getString(R.styleable.MyTiltleView_btn_right_text);

        View view = inflate(context, R.layout.title_layout, this);

        //获取id
        Button left_btn = view.findViewById(R.id.btn_left);
       Button right_btn= view.findViewById(R.id.btn_right);
        TextView text_title=view.findViewById(R.id.title_tet);
        left_btn.setOnClickListener(this);
        right_btn.setOnClickListener(this);
        text_title.setOnClickListener(this);
    }

    //点击事件
    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.btn_left:
                 if(onBtnOnClick!=null){
                     onBtnOnClick.onLeftClick();
                 }
                break;
                 case R.id.btn_right:
                     if(onBtnOnClick!=null){
                         onBtnOnClick.onRIghtClick();
                     }
                     break;

                     case R.id.title_tet:
                         if(onBtnOnClick!=null){
                             onBtnOnClick.getText();
                         }
                         break;
        }
    }


    //接口回调
    public interface onBtnOnClick{

        void onLeftClick();
        void onRIghtClick();
        void getText();
    }
   //设置外部访问的方法
   private onBtnOnClick onBtnOnClick;

    public void onBtnOnClick(MyTitleView.onBtnOnClick onBtnOnClick){

    this.onBtnOnClick=onBtnOnClick;

    }
}







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

<declare-styleable name="MyTiltleView">

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

    <attr name="btn_left_text" format="string"></attr>
    <attr name="btn_right_text" format="string"></attr>


</declare-styleable>
</resources>





//布局

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:layout_marginLeft="20dp"
        />

    <TextView
        android:id="@+id/title_tet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空"
        android:layout_marginLeft="20dp"
        android:gravity="center"

        />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:layout_marginLeft="20dp"
        />
</LinearLayout>
 

猜你喜欢

转载自blog.csdn.net/chenyibai/article/details/80643330