Android使用开源组件PagerBottomTabStrip实现底部菜单和顶部导航功能

时间:2021-05-20

PagerBottomTabStrip 是一个基本按谷歌Material Design规范完成的安卓底部导航栏控件

官方设计规范:https:///apk/res/android" android:id="@+id/top_all" android:layout_width="match_parent" android:background="@color/white" android:orientation="vertical" android:focusable="true" android:focusableInTouchMode="true" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/top_navigation" android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" android:background="@color/grey" android:gravity="center_vertical"> <!--上方导航条返回按钮--> <LinearLayout android:id="@+id/back_btn" android:layout_width="0dp" android:layout_weight="1" android:orientation="horizontal" android:gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:src="@drawable/back_btn" android:layout_marginLeft="5dp" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/navication_text" android:layout_width="0dp" android:layout_weight="5" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="首页" android:textColor="@color/font_title" android:textSize="17dp"/> <!--文字显示--> <TextView android:id="@+id/second_transfer_text" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:text="提交" android:textColor="@color/blue" android:visibility="invisible" android:textSize="@dimen/text_size_14" android:layout_height="match_parent" /> </LinearLayout> <TextView android:layout_width="match_parent" android:background="@color/blue" android:layout_height="@dimen/px_2" /></LinearLayout>

(3)在BaseActivity中写方法

protected void setTitle(Object title,Boolean right,Object rightContent) { try { TextView titleText=findViewById(R.id.navication_text); titleText.setText((String)title); //显示右侧的文字按钮 if(right){ TextView rightText=findViewById(R.id.second_transfer_text); rightText.setVisibility(View.VISIBLE); rightText.setText((String)rightContent); } } catch (Exception e) { e.printStackTrace(); }}/** * 设置点击左上角的返回事件.默认是finish界面 */protected void registerBack() { LinearLayout llLeft = findViewById(R.id.back_btn); llLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BaseActivity.this.finish(); } });}

(4)继承BaseActivity,xml包含includetop.xml然后直接执行方法

<include layout="@layout/top"/>setTitle("首頁",false,null)registerBack()

4、总结

(1)好了,一个简单的底部菜单导航栏就做好了,是不是要比自己写挨个的点击事件要简单许多呢。

(2)在上里面的代码中我们使用了矢量图<Vector>功能,实现标准是美工提供.svg文件,通过studio直接转为xml文件的图片,优势是可伸缩和完美放大,体积小,需要知道一下。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章