Android实现自定义标题栏的方法

所属分类: 软件编程 / Android 阅读数: 803
收藏 0 赞 0 分享

本文要讲自己定义一个标题栏,能加事件。然后可以移值到不同的手机上,基本上不用改什么,调用也很简单
在layout文件夹下,新建一个XML。名字叫做layout_title_bar.xml然后来看看布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="45.0dip" 
  android:background="@drawable/bg_title_bar" 
  android:gravity="top" > 
 
  <ImageView 
    android:id="@+id/title_bar_menu_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="3.0dip" 
    android:layout_marginRight="3.0dip" 
    android:layout_marginTop="3.0dip" 
    android:gravity="center" 
    android:src="@drawable/ic_top_bar_category" /> 
 
  <ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_toRightOf="@id/title_bar_menu_btn" 
    android:background="@drawable/ic_top_divider" /> 
 
  <TextView 
    android:id="@+id/title_bar_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:ellipsize="end" 
    android:gravity="center" 
    android:paddingLeft="75.0dip" 
    android:paddingRight="75.0dip" 
    android:singleLine="true" 
    android:text="Java学习宝典" 
    android:textColor="#ffffff" 
    android:textSize="22sp" /> 
 
</RelativeLayout> 

看下效果:

接下要就是要用了,在要用到的地方:我这里是activity_main.xml文件中:
加上一句:  <include layout="@layout/layout_title_bar" />这样就行了,
然后我们要给标题栏上的按钮添加事件,这个更加简单了:
在MainActivity.java(对应activity_main.xml)中,onCreate函数中添加:事件可以自己改,我这里是让它控制左右滑动的功能。

ImageView menuImg = (ImageView) findViewById(R.id.title_bar_menu_btn); 
    menuImg.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
      public void onClick(View arg0) { 
        if (!menuIsShow) 
          showMenu(); 
        else { 
          hideMenu(); 
        } 
 
      } 
 
    }); 

这样就可以了:
我们来看看效果

这就是效果了,很简单吧,想用直接把上面的布局复制过去就OK了!

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多