自定义标题栏以及点击事件

本文介绍了如何在Android开发中创建自定义的标题栏布局,并实现点击事件的监听与接口回调。通过解析attr.xml文件来定制样式,自定义的类中包含单击事件的处理逻辑,同时提供了在主应用中引用和使用自定义View的示例。
摘要由CSDN通过智能技术生成

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TobBar">
        <attr name="ltftBackGround" format="reference"/>
        <attr name="rightBackGround" format="reference"/>
        <attr name="titleText" format="string"/>
        <attr name="titleSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>
    </declare-styleable>

</resources>

自定义标题的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/left"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        />
    <Button
        android:id="@+id/right"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        />
</RelativeLayout>

自定义的类 以及单击事件接口回调

package com.bwie.space01.zidingyi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bwie.space01.R;
import java.util.ArrayList;

public class TopBar extends RelativeLayout
{

    private final Button left;
    private final Button right;
    private final TextView text;
    OnLeftAndRightClickListene listene;
    public void OnLeftAndRightClickListene(OnLeftAndRightClickListene listene) {
        this.listene =listene;
    }
    //创建接口
    public interface OnLeftAndRightClickListene{
        void   onLeftClick();
        void   onRightClick();
    }

    public void setLeft(boolean flag)
    {
        if(flag)
        {
            left.setVisibility(View.VISIBLE);
        }else{
            left.setVisibility(View.GONE);
        }
    }
    public void setRight(boolean flag)
    {
        if(flag)
        {
            right.setVisibility(View.VISIBLE);
        }else{
            right.setVisibility(View.GONE);
        }
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //找到xml
        LayoutInflater.from(context).inflate(R.layout.bar,this);
        left = findViewById(R.id.left);
        right = findViewById(R.id.right);
        text = findViewById(R.id.text);
        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listene!=null)
                {
                    listene.onLeftClick();
                }

            }
        });
        right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listene!=null)
                {
                    listene.onRightClick();
                }
            }
        });
        //获得自定义属性并赋值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TobBar);
        //得到左边的按钮
        int leftBackGround = typedArray.getResourceId(R.styleable.TobBar_ltftBackGround, 0);
        //得到右边的按钮
        int rightBackGround = typedArray.getResourceId(R.styleable.TobBar_rightBackGround, 0);
        //得到zi
        String string = typedArray.getString(R.styleable.TobBar_titleText);
        float textSize = typedArray.getDimension(R.styleable.TobBar_titleSize, 0);
        int color = typedArray.getColor(R.styleable.TobBar_titleTextColor, 0x38ad5a);
        //一定要释放资源
        typedArray.recycle();
        left.setBackgroundResource(leftBackGround);
        right.setBackgroundResource(rightBackGround);
        text.setText(string);
        text.setTextSize(textSize);
        text.setTextColor(color);
    }
}

引用自定义view的引用

package com.bwie.space01.show;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Toast;

import com.bwie.space01.R;
import com.bwie.space01.zidingyi.TopBar;

public class ShouYe extends Fragment
{
    private TopBar topBar;
    private RecyclerView rec;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shouye, container, false);
        topBar = view.findViewById(R.id.topBar);
        //找到rec控件
        rec = view.findViewById(R.id.rec);
        topBar.OnLeftAndRightClickListene(new TopBar.OnLeftAndRightClickListene() {
            @Override
            public void onLeftClick() {
                Toast.makeText(getActivity(),"点击了左边",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onRightClick() {
                Toast.makeText(getActivity(),"点击了右边",Toast.LENGTH_SHORT).show();
            }
        });
        return view;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值