简介:
在Android开发中,一个activity界面通常都会有一个顶部导航栏,有的是,左边一个返回按钮,中间一个标题,有的,右边还要几个文字,或者一个图标,更有甚者,左边有一个返回图标加几个文字,这种设计在android屡见不鲜,为了减少重复代码,我特别封装了一个控件commontopbar
git地址:https://github.com/wypeng2012/commontopbar
欢迎star和提出问题
效果图如下:
使用方式:
在xml里面使用
1
2
3
4
5
6
<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实现一个接口
1
2
3
4
5
public interface CommonTopBarClick {
void onClickLeft();//click left view on CommonTopBar
void onClickRight();//click right view on CommonTopBar
}设置监听
1
2
3
mCommonTopBar = (CommonTopBar) findViewById(R.id.common);
mCommonTopBar.setCommonTopBarClick(this);//of course you can not set it显示或者隐藏view
1
2
3
mCommonTopBar.isShowLeftView(true);
mCommonTopBar.isShowRightView(true);设置文字
1
2
3
4
5
6
7
8
9
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设置图片
1
2
3
4
5
6
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设置文字大小
1
2
3
4
5
6
setLeftTextSize(float size) //set left text size
setMidTextSize(float size) //set mid text size
setRightTextSize(float size) //set right text size设置文字颜色
1
2
3
4
5
6
7
8
9
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
1
2
3
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的字数长度
1
2
setMidTextMaxLenth(int lenth) //set middle text max length设置左边文字左边的图标
1
2
3
4
setLefTextViewDrawPadding(int size) ;//set DrawPadding
setLeftTextViewDrawLeft(int drawableRes);//set left drawImg
setLeftTextViewDrawLeft(//set left drawImg Drawable drawableRes);
如何远程依赖:
- Maven
1 |
|
- Gradle
1 | compile 'party.loveit:commontopbarlibrary:1.0.1' |
- Ivy
1 |
|