电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android自定義ToolBar并實現沉浸式的方法

瀏覽:4日期:2022-09-24 11:08:32

前言

  ToolBar是Android 5.0推出的一個新的導航控件用于取代之前的ActionBar,由于其高度的可定制性、靈活性、具有Material Design風格等優點,越來越多的App也用上了ToolBar。

  沉浸式狀態欄是從android Kitkat(Android 4.4)開始出現的,它可以被設置成與APP頂部相同的顏色,這就使得切換APP時,整個界面就好似切換到了與APP相同的風格樣式一樣。

依賴包:  Toolbar, implementation ’androidx.appcompat:appcompat:1.1.0’

  沉浸式, implementation ’com.gyf.immersionbar:immersionbar:3.0.0’

1、自定義Toolbar步驟:

1)、定義 /values/styles.xml

<?xml version='1.0' encoding='utf-8'?><resources> <style name='TextAppearance_TitleBar_Title' parent='TextAppearance.AppCompat.Toolbar.Title'> ... </style> <style name='TextAppearance_TitleBar_subTitle' parent='TextAppearance.AppCompat.Toolbar.Subtitle'> .... </style></resources>

2)、自定義toolbar 繼承 androidx.appcompat.widget.Toolbar

public class CustomToolBar extends Toolbar { private TextView mCenterTitle; private ImageView mCenterIcon; //中心icon private TextView mLeftText; private ImageButton mLeftIcon; private TextView mSettingText; private ImageButton mSettingIcon; public CustomToolBar(Context context) { super(context); } public CustomToolBar(Context context, AttributeSet attrs) { super(context, attrs); } public CustomToolBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setLeftText(@StringRes int id) { setLeftText(this.getContext().getText(id)); } public CustomToolBar setLeftText(CharSequence text) { Context context = this.getContext(); if (this.mLeftText == null) { this.mLeftText = new TextView(context); this.mLeftText.setGravity(Gravity.CENTER_VERTICAL); this.mLeftText.setSingleLine();// this.mLeftText.setEllipsize(TextUtils.TruncateAt.END); setLeftTextAppearance(getContext(), R.style.TextAppearance_TitleBar_subTitle); //textView in left// this.addMyView(this.mLeftText, Gravity.START); int i = dp2px(this, 16); this.addMyView(this.mLeftText, Gravity.START, 0, 0, i, 0, 48); } mLeftText.setText(text); return this; } public void setLeftTextAppearance(Context context, @StyleRes int resId) { if (this.mLeftText != null) { this.mLeftText.setTextAppearance(context, resId); } } public void setLeftTextColor(@ColorInt int color) { if (this.mLeftText != null) { this.mLeftText.setTextColor(color); } } public void setLeftTextOnClickListener(OnClickListener listener) { if (mLeftText != null) { mLeftText.setOnClickListener(listener); } } public CustomToolBar setLeftIcon(@DrawableRes int resId) { return setLeftIcon(ContextCompat.getDrawable(this.getContext(), resId)); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public CustomToolBar setLeftIcon(Drawable drawable) { Context context = this.getContext(); if (this.mLeftIcon == null) { this.mLeftIcon = new ImageButton(context); ... this.addMyView(this.mLeftIcon, Gravity.START); } else { if (mLeftIcon.getVisibility() != VISIBLE) { mLeftIcon.setVisibility(VISIBLE); } } if (mLeftText != null && mLeftText.getVisibility() != GONE) { mLeftText.setVisibility(GONE); } mLeftIcon.setImageDrawable(drawable); return this; } public void setLeftIconOnClickListener(OnClickListener listener) { if (mLeftIcon != null) { mLeftIcon.setOnClickListener(listener); } } public void setLeftIconVisibility(int visibility) { if (mLeftIcon != null) { mLeftIcon.setVisibility(visibility); } } public CustomToolBar setMyCenterTitle(@StringRes int id, boolean center) { return setMyCenterTitle(this.getContext().getText(id), center); } public void setMyCenterTitle(@StringRes int Rid) { setMyCenterTitle(this.getContext().getText(Rid)); } public void setMyCenterTitle(CharSequence text) { Context context = this.getContext(); if (this.mCenterTitle == null) { this.mCenterTitle = new TextView(context); ... } else { if (this.mCenterTitle.getVisibility() != VISIBLE) { mCenterTitle.setVisibility(VISIBLE); } } if (mCenterIcon != null && mCenterIcon.getVisibility() != GONE) { mCenterIcon.setVisibility(GONE); } ... } public CustomToolBar setMyCenterTitle(CharSequence text, boolean center) { Context context = this.getContext(); if (this.mCenterTitle == null) { this.mCenterTitle = new TextView(context); ... } else { if (this.mCenterTitle.getVisibility() != VISIBLE) { mCenterTitle.setVisibility(VISIBLE); } } if (mCenterIcon != null && mCenterIcon.getVisibility() != GONE) { mCenterIcon.setVisibility(GONE); } if (!center) { setTitle(text); setTitleTextColor(getResources().getColor(R.color.black)); } else { mCenterTitle.setText(text); mCenterTitle.setTextColor(getResources().getColor(R.color.black)); mCenterTitle.setTextSize(16); } return this; } public void setMyCenterTextAppearance(Context context, @StyleRes int resId) { if (this.mCenterTitle != null) { this.mCenterTitle.setTextAppearance(context, resId); } } public void setMyCenterTextColor(@ColorInt int color) { if (this.mCenterTitle != null) { this.mCenterTitle.setTextColor(color); } } public void setMyCenterTextOnClickListener(OnClickListener listener) { if (mCenterTitle != null) { mCenterTitle.setOnClickListener(listener); } } public void setMyCenterIcon(@DrawableRes int resId) { setMyCenterIcon(ContextCompat.getDrawable(this.getContext(), resId)); } public void setMyCenterIcon(Drawable drawable) { Context context = this.getContext(); if (this.mCenterIcon == null) { ... } else { if (mCenterIcon.getVisibility() != VISIBLE) { mCenterIcon.setVisibility(VISIBLE); } } if (mCenterTitle != null && mCenterTitle.getVisibility() != GONE) { mCenterTitle.setVisibility(GONE); } setTitle(''); mCenterIcon.setImageDrawable(drawable); } public void setMySettingText(@StringRes int Rid) { setMySettingText(this.getContext().getText(Rid)); } public void setMySettingText(CharSequence text) { Context context = this.getContext(); if (this.mSettingText == null) { ... } else { if (mSettingText.getVisibility() != VISIBLE) { mSettingText.setVisibility(VISIBLE); } } if (mSettingIcon != null && mSettingIcon.getVisibility() != GONE) { mSettingIcon.setVisibility(GONE); } mSettingText.setText(text); mSettingText.setTextSize(14); mSettingText.setTextColor(getResources().getColor(R.color.toolbar_title)); } public void setMySettingTextAppearance(Context context, @StyleRes int resId) { if (mSettingText != null) { mSettingText.setTextAppearance(context, resId); } } public void setMySettingTextColor(@ColorInt int color) { if (mSettingText != null) { mSettingText.setTextColor(color); } } public void setSettingTextOnClickListener(OnClickListener listener) { if (mSettingText != null) { mSettingText.setOnClickListener(listener); } } public CustomToolBar setMySettingIcon(@DrawableRes int resId) { return setMySettingIcon(ContextCompat.getDrawable(this.getContext(), resId));// ViewConfiguration.get(this.getContext()).getScaledTouchSlop(); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public CustomToolBar setMySettingIcon(Drawable drawable) { Context context = this.getContext(); if (this.mSettingIcon == null) { ... } else { if (mSettingIcon.getVisibility() != VISIBLE) { mSettingIcon.setVisibility(VISIBLE); } } if (mSettingText != null && mSettingText.getVisibility() != GONE) { mSettingText.setVisibility(GONE); } mSettingIcon.setImageDrawable(drawable); return this; } public void setSettingIconOnClickListener(OnClickListener listener) { if (mSettingIcon != null) { mSettingIcon.setOnClickListener(listener); } } private void addSimpleView(View v, int gravity) { addSimpleView(v, gravity, 0, 0, 0, 0); } private void addSimpleView(View v, int gravity, int left, int top, int right, int bottom) { LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, gravity); lp.setMargins(left, top, right, bottom); this.addView(v, lp); } private void addMyView(View v, int gravity) { addMyView(v, gravity, 0, 0, dp2px(this, 16), 0); } private void addMyView(View v, int gravity, int left, int top, int right, int bottom) { LayoutParams lp = new LayoutParams(dp2px(this, 20), dp2px(this, 20), gravity); lp.setMargins(left, top, right, bottom); this.addView(v, lp); } private void addMyView(View v, int gravity, int left, int top, int right, int bottom, int width) { LayoutParams lp = new LayoutParams(dp2px(this, width), 20, gravity); lp.setMargins(left, top, right, bottom); this.addView(v, lp); } public CustomToolBar setCenterTitleWithImg(CharSequence text, Drawable drawable, boolean center) { Context context = this.getContext(); if (this.mCenterTitle == null) { this.mCenterTitle = new TextView(context); ... if (this.mCenterTitle.getVisibility() != VISIBLE) { mCenterTitle.setVisibility(VISIBLE); } } if (this.mCenterIcon == null) { this.mCenterIcon = new ImageView(context); ... } else { if (mCenterIcon.getVisibility() != VISIBLE) { mCenterIcon.setVisibility(VISIBLE); } } mCenterTitle.setTextSize(18); mCenterTitle.setTextColor(getResources().getColor(R.color.black)); mCenterTitle.setText(text); mCenterIcon.setImageDrawable(drawable); return this; } public void setCenterTitleWithImgOnClickListener(OnClickListener listener) { if (mCenterTitle != null) { ((View) mCenterTitle.getParent()).setOnClickListener(listener); } }}

2、自定義Toolbar使用

1)、res/layout創建布局文件

<?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='wrap_content'> <com.ktx.view.CustomToolBar android: android:layout_width='match_parent' android:layout_height='?actionBarSize' android:background='@android:color/white' app:popupTheme='@style/ThemeOverlay.AppCompat.Light' app:titleTextColor='@android:color/black' /></FrameLayout>

2)、在布局中使用

<?xml version='1.0' encoding='utf-8'?><layout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:binding='http://schemas.android.com/tools'> <data> <variable name='viewModel' type='com.android.playandroid.viewmodel.LoginViewModel' /> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width='match_parent' android:layout_height='match_parent' android:background='@color/bkg' android:orientation='vertical'> <include layout='@layout/title_layout' app:layout_constraintLeft_toLeftOf='parent' app:layout_constraintTop_toTopOf='parent' /> ... </androidx.constraintlayout.widget.ConstraintLayout></layout>

3)、代碼中使用

a、初始化:

mBinding.root.toolbar.setNavigationIcon(R.mipmap.register_close) mBinding.root.toolbar.setMyCenterTitle(getString(R.string.register), true) mBinding.root.toolbar.setMySettingText(getString(R.string.login))

b、點擊事件:

mBinding.root.toolbar.setNavigationOnClickListener { .... } mBinding.root.toolbar.setSettingTextOnClickListener { ...}

4)、沉浸式,設置toolbar背景顏色、文字顏色,一般寫在基類

protected open fun initImmersionBar() { //在BaseActivity里初始化 mImmersionBar = ImmersionBar.with(this) if (toolbar != null) { mImmersionBar.titleBar(toolbar) } mImmersionBar.statusBarDarkFont(true) // mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init() // mImmersionBar.init()ImmersionBar.with(this).init() }

Google原生的效果,不必多說,可以實現類似這樣的效果

Android自定義ToolBar并實現沉浸式的方法

注意:

1、配置整個app的toolbar風格,在/value/styles.xml文件修改代碼

<style name='AppTheme' parent='Theme.AppCompat.Light.NoActionBar'> <item name='colorPrimary'>@android:color/white</item> <item name='colorPrimaryDark'>@android:color/white</item> <item name='colorAccent'>@android:color/white</item> ... </style>

2、修改了 toolbar的高度 ,怎么讓navigationIcon顯示在toolbar中心?

  只要設置如下,即可:android:minHeight='@dimen/toolbar_height'

3、toolbar布局文件位置

  如果在commonlibrary目錄創建該文件,在app 下還需要復制一份,因為在app 使用toolbar,kotlin-android-extensions引用不到commonlibrary目錄下的布局文件。

代碼Github:https://github.com/AlbertShen0211/PlayAndroid

總結

到此這篇關于Android自定義ToolBar并實現沉浸式的文章就介紹到這了,更多相關Android 自定義ToolBar并沉浸式內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | cnc精密加工_数控机械加工_非标平键定制生产厂家_扬州沃佳机械有限公司 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 税筹星_灵活用工平台_企业财务顾问_财税法薪综合服务平台 | 活性炭厂家-蜂窝活性炭-粉状/柱状/果壳/椰壳活性炭-大千净化-活性炭 | 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 合肥汽车充电桩_安徽充电桩_电动交流充电桩厂家_安徽科帝新能源科技有限公司 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 太阳能发电系统-太阳能逆变器,控制器-河北沐天太阳能科技首页 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 电池高低温试验箱-气态冲击箱-双层电池防爆箱|简户百科 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 金联宇电缆总代理-金联宇集团-广东金联宇电缆实业有限公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 科普仪器菏泽市教育教学仪器总厂 | 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 丹佛斯变频器-Danfoss战略代理经销商-上海津信变频器有限公司 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 带式压滤机_污泥压滤机_污泥脱水机_带式过滤机_带式压滤机厂家-河南恒磊环保设备有限公司 | 办公室装修_上海办公室设计装修_时尚办公新主张-后街印象 | 慢回弹测试仪-落球回弹测试仪-北京冠测精电仪器设备有限公司 | 期货软件-专业期货分析软件下载-云智赢 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 合同书格式和范文_合同书样本模板_电子版合同,找范文吧 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 西门子伺服控制器维修-伺服驱动放大器-828D数控机床维修-上海涌迪 | 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 南京办公用品网-办公文具用品批发-打印机耗材采购 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 |