市面上大部分手机app都在底部设有快捷导航栏的功能,因此我在查找相关资料后整理出了导航栏相关代码设计。
<LinearLayout
android:id="@+id/bottom_tab"
android:background="#bbbbff"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_weight="1"
android:id="@+id/bottom_tab_1"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_above="@+id/bottom_tab_1_text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:id="@+id/bottom_tab_1_image"
android:src="@drawable/ic_bottomatab_index"/>
<TextView
android:id="@+id/bottom_tab_1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:gravity="center"
android:singleLine="true"
android:text="@string/index_button"
android:textColor="#FFFFFF"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="1"
android:id="@+id/bottom_tab_2"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_above="@+id/bottom_tab_2_text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:id="@+id/bottom_tab_2_image"
android:src="@drawable/ic_bottomatab_edit"/>
<TextView
android:id="@+id/bottom_tab_2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:gravity="center"
android:singleLine="true"
android:text="@string/operation_button"
android:textColor="#FFFFFF"
android:textSize="14sp"/>
</RelativeLayout>
</LinearLayout>
这是我编写的一个简易导航栏XML代码,注意父级Layout的布局样式必须为RelativeLayout,否则layout_alignParentBottom属性会失效。
mPagerBottomTabStrip.addOnPageChangeListener(Listener);
监听ViewPager的滑动变化事件。
DefaultMode() //默认模式:显示原始的图标,不做颜色上的修改
ColorMode() //纯色模式:改变图标的颜色为设置的颜色
TabTextSize(int textsize)//文字大小
TabPadding(int padding) //内边距,单位为DP
TabTextColor(int textcolor) //文字颜色
TabClickTextColor(int clicktextcolor) //选中之后文字的颜色
TabIconColor(int iconcolor)// 在COLORMODE模式下图标的颜色
TabClickIconColor(int clickiconcolor) //在COLORMODE模式下选中后图标的颜色
TabMessageBackgroundColor(int backgroundcolor) //圆形消息的背景颜色
TabMessageTextColor(int textcolor) //圆形消息的数字颜色
TabIcon(int[] resids) //设置图标,非XML文件
TabClickIcon(int[] resids) //设置选中后的图标,非XML文件
build()//使用此方法完成构建
setMessageNumber(int postion,int messageNumber)
设置按钮显示的消息数量
postion 第几个按钮,从0开始。
messageNumber 需要显示的消息数量
此方法可用于大多数app都有的小红点通知功能
addMessageNumber(int postion,int messageNumber)
增减按钮显示的消息数量
postion 第几个按钮,从0开始。
messageNumber 需要增减的消息数量
setNews(int postion,Boolean news)
控制无数字小圆点的显示与消失
news false消失,true显示。
postion 第几个按钮,从0开始。
addOnPageChangeListener(OnPageChangeListener listener)
增加ViewPager滑动监听
应用这些布局和方法能快速构建出一个可用的导航栏。