MVP 替换标题栏,并在左右添加点击事件

kaoshi_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="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/left_btn"
        android:layout_width="50dp"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_weight="1"
        android:gravity="center"
        android:text="11111"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/right_btn"
        android:layout_width="50dp"
        android:layout_height="wrap_content" />

</LinearLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <com.day04.TitleView
        android:id="@+id/titleview_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.day04.TitleView>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/colorAccent" />

</LinearLayout>



TitleView.java

package com.day04;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by lenovo on 2017/11/03.
 */

public class TitleView extends LinearLayout {
    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // setContentView
        View inflate = LayoutInflater.from(context).inflate(R.layout.kaoshi_title_layout, this);
        Button left = (Button) inflate.findViewById(R.id.left_btn);
        Button right = (Button) inflate.findViewById(R.id.right_btn);

        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.leftClick();
            }
        });

        right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.rightClick();
            }
        });

    }

    public LeftCallBack callBack;
    public void setCallBack(LeftCallBack callBack) {
        this.callBack = callBack;
    }

    interface LeftCallBack {
        public void leftClick();

        public void rightClick();
    }
}



MainActivity.java

package com.day04;

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

public class MainActivity extends Activity implements TitleView.LeftCallBack {

    public TitleView titleView;

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

        titleView = (TitleView) findViewById(R.id.titleview_id);
        titleView.setCallBack(this);
    }

    @Override
    public void leftClick() {
        Toast.makeText(this, "leftClick", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void rightClick() {
        Toast.makeText(this, "rightClick", Toast.LENGTH_SHORT).show();
    }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值