Android自定义通用标题栏CustomTitleBar

yizhihongxing

下面是详细的Android自定义通用标题栏CustomTitleBar的攻略:

一、背景介绍

在很多Android应用中都会有通用的标题栏,包括应用名称、返回按钮、菜单按钮等等。这些通用的功能可以通过自定义通用标题栏来实现,这样不仅可以提高应用的美观程度,还可以增强用户的体验感。

二、实现方式

实现自定义通用标题栏有多种方式,这里介绍两种比较常用的方式。

1. 自定义 XML 文件实现

第一步,创建一个布局文件,命名为custom_title_bar.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_title"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary">

    <ImageButton
        android:id="@+id/btn_title_left"
        android:layout_width="?android:attr/actionBarSize"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="centerInside"
        android:padding="@dimen/default_padding"
        android:src="@drawable/ic_keyboard_arrow_left_white_24dp"
        android:contentDescription="@string/back"
        />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_normal"
        android:text="@string/app_name"
        />

    <ImageButton
        android:id="@+id/btn_title_right"
        android:layout_width="?android:attr/actionBarSize"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="centerInside"
        android:padding="@dimen/default_padding"
        android:src="@drawable/ic_menu_white_24dp"
        android:contentDescription="@string/menu"
        />

</RelativeLayout>

这里使用了RelativeLayout布局,包含了左边的返回按钮、中间的应用名称和右边的菜单按钮,注意在ImageButton中要设置android:background="?attr/selectableItemBackgroundBorderless"以便提高用户的体验。

第二步,将自定义的标题栏加入Activity中,代码如下:

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

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_title_bar);
    getSupportActionBar().setElevation(0);
}

这里使用了AppCompat中的getSupportActionBar()方法获取ActionBar,并通过setDisplayOptions、setCustomView和setElevation方法实现自定义标题栏。

第三步,监听按钮事件,代码如下:

private void initTitleBar() {
    ImageButton btnLeft = findViewById(R.id.btn_title_left);
    btnLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    ImageButton btnRight = findViewById(R.id.btn_title_right);
    btnRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showMenuPopup();
        }
    });
}

这里通过findViewById方法获取ImageButton的ID,并通过setOnClickListener监听按钮的点击事件,在点击事件中实现相应的逻辑。

2. 自定义 Java 类实现

第一步,创建一个自定义标题栏类CustomTitleBar,代码如下:

public class CustomTitleBar extends RelativeLayout {
    private ImageButton mBtnLeft;
    private TextView mTvTitle;
    private ImageButton mBtnRight;

    public CustomTitleBar(Context context) {
        super(context);
        initView(context);
    }

    public CustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_title_bar, this);
        mBtnLeft = view.findViewById(R.id.btn_title_left);
        mTvTitle = view.findViewById(R.id.tv_title);
        mBtnRight = view.findViewById(R.id.btn_title_right);
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setLeftButton(int resId, OnClickListener listener) {
        mBtnLeft.setImageResource(resId);
        mBtnLeft.setOnClickListener(listener);
    }

    public void setRightButton(int resId, OnClickListener listener) {
        mBtnRight.setImageResource(resId);
        mBtnRight.setOnClickListener(listener);
    }
}

这里通过继承RelativeLayout类,实现自定义标题栏的功能。使用了initView方法将自定义布局文件custom_title_bar.xml加入到CustomTitleBar中,并通过findViewById方法获取内部的ImageButton和TextView。

为了方便外部的Activity调用,提供了setTitle、setLeftButton和setRightButton三个方法,分别用于设置标题、设置左边的返回按钮和设置右边的菜单按钮。

第二步,将自定义标题栏加入Activity中,代码如下:

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

    CustomTitleBar customTitleBar = findViewById(R.id.title_bar_layout);
    customTitleBar.setTitle(getString(R.string.app_name));
    customTitleBar.setLeftButton(R.drawable.ic_keyboard_arrow_left_white_24dp, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    customTitleBar.setRightButton(R.drawable.ic_menu_white_24dp, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showMenuPopup();
        }
    });
}

这里通过findViewById方法获取自定义标题栏CustomTitleBar的ID,并使用setTitle、setLeftButton和setRightButton方法设置标题、左侧按钮和右侧按钮的图片和点击事件。

三、总结

通过以上两种方式的介绍,我们可以实现自定义通用标题栏。两种方式的实现方法不同,但是都可以实现相同的功能,在实际开发中可以根据自己的需要选择适合自己的实现方式。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android自定义通用标题栏CustomTitleBar - Python技术站

(0)
上一篇 2023年6月25日
下一篇 2023年6月25日

相关文章

  • tcp发送窗口更新tcp_ack_update_window

    TCP发送窗口更新tcp_ack_update_window TCP是一种面向连接的协议,用于在网络上可靠地传输数据。在TCP连接中,发送方和接收方之间会进行数据传输确认。为了提高传输效率,TCP使用了发送窗口和接收窗口的机制。本文将提供一个完整的攻略,介绍TCP发送窗口更新tcp_ack_update_window的基础知识,并提供两个示例说明。 TCP发…

    other 2023年5月8日
    00
  • Linux UDP服务端和客户端程序的实现

    下面是关于Linux UDP服务端和客户端程序的实现的详细攻略。 1.UDP简介 UDP(User Datagram Protocol)用户数据报协议是一种无连接的协议,与TCP协议不同,UDP不会建立连接,发送数据时不会保证数据的可靠性以及顺序,甚至不保证是否到达对方。UDP在实时数据传输中非常常见,例如视频流、音频流等。 2.UDP服务端程序实现 下面的…

    other 2023年6月27日
    00
  • Windows Powershell 命令集 cmdlets

    Windows Powershell 命令集 cmdlets 完整攻略 简介 Windows Powershell 是一种强大的命令行工具,可以通过 cmdlets(核心模块)来实现各种任务,包括系统管理、脚本编写、数据分析等。Cmdlets 可以实现复杂的任务,还能通过管道将命令链接起来,完成更加强大的功能。 Cmdlets 的结构 Cmdlets 具有以…

    other 2023年6月26日
    00
  • 惠普M436打印机怎么重启? 打印机重启的教程

    惠普M436打印机重启教程 1.为什么要重启惠普M436打印机? 在使用惠普M436打印机时,有时会遇到打印机出现各种问题的情况,比如打印机卡纸、打印质量不佳等。此时,我们可以首先尝试重启打印机,这通常可以解决一些简单的技术问题。 2.惠普M436打印机的重启方法 以下是重启惠普M436打印机的步骤: 步骤1:按下电源按钮 首先,让我们找到位于惠普M436打…

    other 2023年6月26日
    00
  • 详解C语言的exp()函数和ldexp()函数以及frexp()函数

    详解C语言的exp()函数和ldexp()函数以及frexp()函数 1. exp()函数 1.1 函数简介 exp()函数是C语言中一个计算指数函数的函数,可以计算以自然对数为底的指数函数。 1.2 函数原型 double exp(double x); 1.3 函数参数 exp()函数的参数x为一个double类型的数值,表示指数。 1.4 函数返回值 e…

    other 2023年6月26日
    00
  • Java网络编程基础篇之单向通信 原创

    当我们开展Java网络编程时,我们首先需要了解的是基础的单向通信。 单向通信是指通信流只能在一条路径上单向发送的通信模式。例如,服务器发送数据到客户端,而客户端不能发送数据回服务器。在 Java 中,单向通信可以通过 Socket 和 ServerSocket 实现。 以下是实现单向通信的步骤: 创建一个ServerSocket对象,使用一个门牌号绑定到一个…

    other 2023年6月27日
    00
  • PHP实现链表的定义与反转功能示例

    下面我将详细讲解“PHP实现链表的定义与反转功能示例”的完整攻略,过程中将包含两条示例说明。 什么是链表 链表是一种常见的数据结构,它由多个节点组成,每个节点存储了数据和指向下一个节点的指针。相比于数组,链表的插入和删除效率更高,但访问操作的效率较低。 PHP实现链表的定义 在PHP中,我们可以使用类来实现链表。首先,我们需要定义一个节点类,代码如下: cl…

    other 2023年6月27日
    00
  • JSP利用freemarker生成基于word模板的word文档

    JSP利用Freemarker生成基于Word模板的Word文档 在现今的信息化环境中,大量的文档处理都需要将生成的信息导出为Word文档,因此,如何在Web应用中实现Word文档的生成和导出成为了开发者们的一大问题。本文就将介绍如何使用JavaServer Pages(JSP)和Freemarker模板引擎来生成基于Word模板的Word文档。 1. JS…

    其他 2023年3月28日
    00
合作推广
合作推广
分享本页
返回顶部