API29下ActionBar和ToolBar的基本实现(1)

本文详细描述了如何在Android应用中自定义ActionBar和ToolBar的布局,包括XML文件的编写,以及如何设置标题、菜单、搜索框等功能。着重展示了ToolBar的MaterialDesign风格和可定制性。
摘要由CSDN通过智能技术生成

<LinearLayout

android:id=“@+id/search”

android:layout_width=“200dp”

android:layout_height=“wrap_content”

android:background=“@drawable/edittext_bg_shape”

android:layout_margin=“3dp”

android:layout_centerInParent=“true”

android:padding=“5dp”

android:visibility=“gone”>

<TextView

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“match_parent”

android:hint=“search”

android:layout_centerInParent=“true”

android:padding=“3dp”

android:textColor=“#fff”

android:textSize=“13sp” />

<RelativeLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:layout_centerVertical=“true”

android:padding=“12dp”>

<TextView

android:id=“@+id/header_menu”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“16sp”

android:textColor=“#fff”/>

<View

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”/>

(3)在activity_main.xml中定义所有actionbar的布局

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

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:context=“com.example.actionbar.customactionbar.MainActivity”>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_0”

android:layout_width=“match_parent”

android:layout_height=“45dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_1”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_2”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_3”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_4”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_5”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

<com.example.actionbar.customactionbar.CustomActionBar

android:id=“@+id/actionbar_6”

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

(4)定义CustomActionBar类,封装了ActionBar的各种函数

public class CustomActionBar extends LinearLayout {

private ImageView headerBack;

private TextView headerTitle, headerMenuText;

private LinearLayout Search;

private LayoutInflater mInflater;

private View headView;

public CustomActionBar(Context context) {

super(context);

init(context);

}

public CustomActionBar(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

public void init(Context context) {

mInflater = LayoutInflater.from(context);

headView = mInflater.inflate(R.layout.customactionbar, null);

addView(headView);

initView();

}

private void initView() {

headerBack = headView.findViewById(R.id.header_back);

headerTitle = headView.findViewById(R.id.header_title);

headerMenuText = headView.findViewById(R.id.header_menu);

Search = headView.findViewById(R.id.search);

headerBack.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

((Activity) getContext()).finish();

}

});

}

public void setStyle(String title) {

if (title != null)

headerTitle.setText(title);

}

/**

  • 标题加文字菜单

  • @param title

  • @param menuText

  • @param listener

*/

public void setStyle(String title, String menuText, OnClickListener listener) {

setStyle(title);

if (menuText != null)

headerMenuText.setText(menuText);

headerMenuText.setOnClickListener(listener);

}

/**

  • 只有右边字体

  • @param menuText

  • @param listener

*/

public void setStyle(String menuText, OnClickListener listener) {

headerBack.setVisibility(GONE);

if (menuText != null)

headerMenuText.setText(menuText);

headerMenuText.setOnClickListener(listener);

}

/**

  • 标题加图标菜单

  • @param title

  • @param menuImgResource

  • @param listener

*/

public void setStyle(String title, int menuImgResource, OnClickListener listener) {

setStyle(title);

headerMenuText.setBackgroundResource(menuImgResource);

headerMenuText.setOnClickListener(listener);

}

public void setStyle(boolean hasSearch){

if(hasSearch){

Search.setVisibility(VISIBLE);

}

}

/**

  • 将默认的返回按钮功能去掉

*/

public void setStyleNoBack(String title) {

setStyle(title);

headerBack.setVisibility(GONE);

}

}

(5)在MainActivity类中实现各种ActionBar

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

private void init() {

CustomActionBar actionBar0 = findViewById(R.id.actionbar_0);

CustomActionBar actionBar1 = findViewById(R.id.actionbar_1);

CustomActionBar actionBar2 = findViewById(R.id.actionbar_2);

CustomActionBar actionBar3 = findViewById(R.id.actionbar_3);

CustomActionBar actionBar4 = findViewById(R.id.actionbar_4);

CustomActionBar actionBar5 = findViewById(R.id.actionbar_5);

CustomActionBar actionBar6 = findViewById(R.id.actionbar_6);

//actionbar0默认只有返回键

//只有标题

actionBar1.setStyleNoBack(“标题”);

//只有菜单

actionBar2.setStyle(“菜单”, new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),“您点击了菜单”,Toast.LENGTH_SHORT).show();

}

});

//返回键 + 标题

actionBar3.setStyle(“标题”);

//返回键 + 标题 + 菜单

actionBar4.setStyle(“标题”, “菜单”, new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),“您点击了菜单”,Toast.LENGTH_SHORT).show();

}

});

//返回键 + 标题 + 菜单图标

actionBar5.setStyle(“标题”, R.mipmap.more_white, new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),“您点击了菜单”,Toast.LENGTH_SHORT).show();

}

});

//搜索框模式

actionBar6.setStyle(true);

}

}

  • 搜索框格式实现:在drawable中创建相应的格式文件,定义如下
<?xml version="1.0" encoding="utf-8"?>

<shape

xmlns:android=“http://schemas.android.com/apk/res/android”>

效果图

ActionBar效果图

自定义ToolBar

=====================================================================

Toolbar 是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件。由于他的可定制度高,所以已经逐步替代掉了ActionBar。实际上这两个可以理解为同一个东西,ToolBar是对ActionBar的升级,使用起来也基本是一样的。

ToolBar可自定义以下几种元素,以适应不同的需求

  • 设置导航栏图标

  • 设置logo

  • 设置标题与子标题

  • 设置菜单(文字及图标菜单)

  • 可自定义添加一至多个控件

(1)编写activity_main.xml文件,定义工具栏的布局

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

<FrameLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<Toolbar

android:id=“@+id/tool_bar”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:minHeight=“?attr/actionBarSize”

android:background=“#FFFFFF”

app:popupTheme=“@style/Theme.Toolbar.base.MenuItem”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Clock”/>

(2)创建自定义的菜单(Menu文件)

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

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”>

<item

android:id=“@+id/menu_search”

android:icon=“@mipmap/ic_search”

android:title=“@string/menu_search”

android:showAsAction=“ifRoom|withText” />

<item

android:id=“@+id/menu_notification”

android:icon=“@mipmap/ic_clock”

android:title=“@string/menu_notification”

android:showAsAction=“ifRoom|withText” />

<item

android:id=“@+id/menu_close”

android:title=“@string/menu_close”

app:showAsAction=“never”/>

<item

Android核心知识点

面试成功其实是必然的,因为我做足了充分的准备工作,包括刷题啊,看一些Android核心的知识点,看一些面试的博客吸取大家面试的一些经验。

下面这份PDF是我翻阅了差不多3个月左右一些Android大博主的博客从他们那里取其精华去其糟泊所整理出来的一些Android的核心知识点,全部都是精华中的精华,我能面试到现在2-2资深开发人员跟我整理的这本Android核心知识点有密不可分的关系,在这里本着共赢的心态分享给各位朋友。

不管是Android基础还是Java基础以及常见的数据结构,这些是无原则地必须要熟练掌握的,尤其是非计算机专业的同学,面试官一上来肯定是问你基础,要是基础表现不好很容易被扣上基础不扎实的帽子,常见的就那些,只要你平时认真思考过基本上面试是没太大问题的。

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上我搜集整理的2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了PDF,包知识脉络 + 诸多细节。

节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
never"/>

<item

Android核心知识点

面试成功其实是必然的,因为我做足了充分的准备工作,包括刷题啊,看一些Android核心的知识点,看一些面试的博客吸取大家面试的一些经验。

下面这份PDF是我翻阅了差不多3个月左右一些Android大博主的博客从他们那里取其精华去其糟泊所整理出来的一些Android的核心知识点,全部都是精华中的精华,我能面试到现在2-2资深开发人员跟我整理的这本Android核心知识点有密不可分的关系,在这里本着共赢的心态分享给各位朋友。

[外链图片转存中…(img-s0JULlck-1714792375762)]

不管是Android基础还是Java基础以及常见的数据结构,这些是无原则地必须要熟练掌握的,尤其是非计算机专业的同学,面试官一上来肯定是问你基础,要是基础表现不好很容易被扣上基础不扎实的帽子,常见的就那些,只要你平时认真思考过基本上面试是没太大问题的。

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上我搜集整理的2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了PDF,包知识脉络 + 诸多细节。

节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值