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

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

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

瀏覽:5日期: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
相關文章:
主站蜘蛛池模板: 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 二手Sciex液质联用仪-岛津气质联用仪-二手安捷伦气质联用仪-上海隐智科学仪器有限公司 | 噪声治理公司-噪音治理专业隔音降噪公司 | sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 鑫达滑石-辽宁鑫达滑石集团 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 金现代信息产业股份有限公司--数字化解决方案供应商 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 奥运星-汽车性能网评-提供个性化汽车资讯 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 电表箱-浙江迈峰电力设备有限公司-电表箱专业制造商 | Duoguan 夺冠集团| 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 膜结构停车棚-自行车棚-膜结构汽车棚加工安装厂家幸福膜结构 | CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 一航网络-软件测评官网 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 方源木业官网-四川木门-全国木门专业品牌 | 国际学校_国际学校哪个好_国际课程学校-国际学校择校网 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 氟塑料磁力泵-不锈钢离心泵-耐腐蚀化工泵厂家「皖金泵阀」 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 海峰资讯 - 专注装饰公司营销型网站建设和网络营销培训 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 超声波反应釜【百科】-以马内利仪器 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 展厅装修公司|企业展厅设计|展厅制作|展厅搭建—广州展厅装饰公司 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 |