简介:

在Android开发中,一个activity界面通常都会有一个顶部导航栏,有的是,左边一个返回按钮,中间一个标题,有的,右边还要几个文字,或者一个图标,更有甚者,左边有一个返回图标加几个文字,这种设计在android屡见不鲜,为了减少重复代码,我特别封装了一个控件commontopbar

git地址:https://github.com/wypeng2012/commontopbar

欢迎star和提出问题

效果图如下:

使用方式:

  1. 在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"/>
  2. 你的activity实现一个接口

    1
    2
    3
    4
    5

    public interface CommonTopBarClick {
    void onClickLeft();//click left view on CommonTopBar
    void onClickRight();//click right view on CommonTopBar
    }
  3. 设置监听

    1
    2
    3

    mCommonTopBar = (CommonTopBar) findViewById(R.id.common);
    mCommonTopBar.setCommonTopBarClick(this);//of course you can not set it
  4. 显示或者隐藏view

    1
    2
    3

    mCommonTopBar.isShowLeftView(true);
    mCommonTopBar.isShowRightView(true);
  5. 设置文字

    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
  6. 设置图片

    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
  7. 设置文字大小

    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
  8. 设置文字颜色

    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
  9. 添加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
  10. 设置中间title的字数长度

    1
    2

    setMidTextMaxLenth(int lenth) //set middle text max length
  11. 设置左边文字左边的图标

    1
    2
    3
    4

    setLefTextViewDrawPadding(int size) ;//set DrawPadding
    setLeftTextViewDrawLeft(@DrawableRes int drawableRes);//set left drawImg
    setLeftTextViewDrawLeft(@NonNull Drawable drawableRes);//set left drawImg

如何远程依赖:

  1. Maven
1
2
3
4
5
6
7

<dependency>
<groupId>party.loveit</groupId>
<artifactId>commontopbarlibrary</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
  1. Gradle
1
compile 'party.loveit:commontopbarlibrary:1.0.1'
  1. Ivy
1
2
3
4

<dependency org='party.loveit' name='commontopbarlibrary' rev='1.0.1'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>