API29下ActionBar和ToolBar的基本实现,android基础面试

文章详细描述了如何在Android项目中使用自定义的CustomActionBar组件,包括XML布局设计和CustomActionBar类的实现,展示了不同样式和功能的设置方法。
摘要由CSDN通过智能技术生成

<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"?>

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

结尾

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频**
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-IDUPlAFs-1711380950528)]

结尾

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

[外链图片转存中…(img-KH7uYsea-1711380950528)]

  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值