简介:
在Android开发中,一个activity界面通常都会有一个顶部导航栏,有的是,左边一个返回按钮,中间一个标题,有的,右边还要几个文字,或者一个图标,更有甚者,左边有一个返回图标加几个文字,这种设计在android屡见不鲜,为了减少重复代码,我特别封装了一个控件commontopbar
github地址:https://github.com/wypeng2012/commontopbar
本人blog文章地址:点我
欢迎star和提出问题
效果图如下:
使用方式:
在xml里面使用
<party.loveit.commontopbar.CommonTopBar android:id="@+id/common" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/top_bar_bg_color"/>
你的activity实现一个接口
public interface CommonTopBarClick { void onClickLeft();//click left view on CommonTopBar void onClickRight();//click right view on CommonTopBar }
设置监听
mCommonTopBar = (CommonTopBar) findViewById(R.id.common); mCommonTopBar.setCommonTopBarClick(this);//of course you can not set it
显示或者隐藏view
mCommonTopBar.isShowLeftView(true); mCommonTopBar.isShowRightView(true);
设置文字
setLeftText(CharSequence charSequence) //set left view text setLeftText(int textRes) //set left view text setMidText(CharSequence charSequence) //set mid view text setMidText(int textRes) //set mid view text setRightText(CharSequence charSequence) //set right view text setRightText(int textRes) //set right view text
设置图片
setLeftImage(android.graphics.drawable.Drawable drawable) //set left image setLeftImage(int drawableRes) //set left image setRightImage(android.graphics.drawable.Drawable drawable) //set right image setRightImage(int drawableRes) //set right image
设置文字大小
setLeftTextSize(float size) //set left text size setMidTextSize(float size) //set mid text size setRightTextSize(float size) //set right text size
设置文字颜色
setLeftTextColor(android.content.res.ColorStateList colorStateList) //set left text color setLeftTextColor(int color) //set left text color setMidTextColor(android.content.res.ColorStateList colorStateList) //set mid text color setMidTextColor(int color) //set mid text color setRightText(int textRes) //set right view text setRightTextColor(android.content.res.ColorStateList colorStateList) //set right text color
添加margin
addLeftMargin(float marginLeft) //set left view margin left,the param is dp addRightMargin(float marginRight) //set right view margin right,the param is dp
设置中间title的字数长度
setMidTextMaxLenth(int lenth) //set middle text max length
设置左边文字左边的图标
setLefTextViewDrawPadding(int size) ;//set DrawPadding setLeftTextViewDrawLeft(@DrawableRes int drawableRes);//set left drawImg setLeftTextViewDrawLeft(@NonNull Drawable drawableRes);//set left drawImg
如何远程依赖:
- Maven
<dependency>
<groupId>party.loveit</groupId>
<artifactId>commontopbarlibrary</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
- Gradle
compile 'party.loveit:commontopbarlibrary:1.0.1'
- Ivy
<dependency org='party.loveit' name='commontopbarlibrary' rev='1.0.1'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>