栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

API29下ActionBar和ToolBar的基本实现,Android开发实用必备的几款插件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

API29下ActionBar和ToolBar的基本实现,Android开发实用必备的几款插件

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

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background="#00BFFF">

android:id="@+id/header_back"

android:layout_width=“26dp”

android:layout_height=“26dp”

android:layout_centerVertical=“true”

android:layout_marginLeft=“5dp”

android:padding=“4dp”

android:src="@mipmap/ic_arrow_back" />

android:id="@+id/linear_header_container"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerInParent=“true”>

android:id="@+id/header_title"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textColor="#fff"

android:textSize=“18sp” />

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”>

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” />

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:layout_centerVertical=“true”

android:padding=“12dp”>

android:id="@+id/header_menu"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“16sp”

android:textColor="#fff"/>

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight="true
"/>

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

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”>

android:id="@+id/actionbar_0"

android:layout_width=“match_parent”

android:layout_height=“45dp”/>

android:id="@+id/actionbar_1"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

android:id="@+id/actionbar_2"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

android:id="@+id/actionbar_3"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

android:id="@+id/actionbar_4"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

android:id="@+id/actionbar_5"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“20dp”/>

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);

}

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

setStyle(title);

if (menuText != null)

headerMenuText.setText(menuText);

headerMenuText.setonClickListener(listener);

}

public void setStyle(String menuText, onClickListener listener) {

headerBack.setVisibility(GONE);

if (menuText != null)

headerMenuText.setText(menuText);

headerMenuText.setonClickListener(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中创建相应的格式文件,定义如下

ionBar4.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中创建相应的格式文件,定义如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号