经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android自定义简单的顶部标题栏
来源:jb51  时间:2018/11/22 9:58:57  对本文有异议

本文实例为大家分享了Android实现简单顶部标题栏的具体代码,供大家参考,具体内容如下

实现功能:

1)自定义View标题栏布局;

2)灵活的可以自己传入类型,选择所需要的控件来显示隐藏

3)相对于我之前写过的一篇,免继承,可直接在布局里使用

4)直接可以在布局控件里设置属性

老规矩,上几张效果图:

由效果图可见,这个是可以根据传入type来控制,比较灵活的

下面就来实现以下步骤,最后我会贴上源码

1.创建一个布局文件,命名,layout_titlebar,来部署我们的标题栏样式,可以自定义更改,图片文件可暂时用自己的替代

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="50dp">
  7. <ImageView
  8. android:id="@+id/iv_back"
  9. android:layout_width="30dp"
  10. android:layout_height="30dp"
  11. android:layout_marginLeft="20dp"
  12. android:src="@drawable/icon_back"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintTop_toTopOf="parent" />
  15. <TextView
  16. android:id="@+id/tv_title"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="标题"
  20. android:textColor="#000"
  21. android:textSize="16sp"
  22. app:layout_constraintBottom_toBottomOf="parent"
  23. app:layout_constraintLeft_toLeftOf="parent"
  24. app:layout_constraintRight_toRightOf="parent"
  25. app:layout_constraintTop_toTopOf="parent" />
  26. <TextView
  27. android:id="@+id/tv_more"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="更多"
  31. android:textColor="#000"
  32. android:textSize="16sp"
  33. app:layout_constraintBottom_toBottomOf="parent"
  34. app:layout_constraintRight_toRightOf="parent"
  35. app:layout_constraintTop_toTopOf="parent" />
  36. <ImageView
  37. android:id="@+id/iv_more"
  38. android:layout_width="30dp"
  39. android:layout_height="30dp"
  40. android:src="@drawable/icon_more"
  41. app:layout_constraintBottom_toBottomOf="parent"
  42. app:layout_constraintRight_toRightOf="parent"
  43. app:layout_constraintTop_toTopOf="parent" />
  44. </android.support.constraint.ConstraintLayout>

2.自定义View,继承自RelativeLayout,第3步贴上attr文件

  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.util.AttributeSet;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.widget.ImageView;
  7. import android.widget.RelativeLayout;
  8. import android.widget.TextView;
  9. /**
  10. * @Author : 张
  11. * @Email : manitozhang@foxmail.com
  12. * @Date : 2018/9/19
  13. *
  14. * 一个简单的自定义标题栏
  15. */
  16. public class CustomTitleBar extends RelativeLayout {
  17. private ImageView ivBack;
  18. private TextView tvTitle;
  19. private TextView tvMore;
  20. private ImageView ivMore;
  21. public CustomTitleBar(Context context, AttributeSet attrs) {
  22. super(context, attrs);
  23. initView(context,attrs);
  24. }
  25. //初始化视图
  26. private void initView(final Context context, AttributeSet attributeSet) {
  27. View inflate = LayoutInflater.from(context).inflate(R.layout.layout_titlebar, this);
  28. ivBack = inflate.findViewById(R.id.iv_back);
  29. tvTitle = inflate.findViewById(R.id.tv_title);
  30. tvMore = inflate.findViewById(R.id.tv_more);
  31. ivMore = inflate.findViewById(R.id.iv_more);
  32. init(context,attributeSet);
  33. }
  34. //初始化资源文件
  35. public void init(Context context, AttributeSet attributeSet){
  36. TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomTitleBar);
  37. String title = typedArray.getString(R.styleable.CustomTitleBar_title);//标题
  38. int leftIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_left_icon, R.drawable.icon_back);//左边图片
  39. int rightIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_right_icon, R.drawable.icon_more);//右边图片
  40. String rightText = typedArray.getString(R.styleable.CustomTitleBar_right_text);//右边文字
  41. int titleBarType = typedArray.getInt(R.styleable.CustomTitleBar_titlebar_type, 10);//标题栏类型,默认为10
  42. //赋值进去我们的标题栏
  43. tvTitle.setText(title);
  44. ivBack.setImageResource(leftIcon);
  45. tvMore.setText(rightText);
  46. ivMore.setImageResource(rightIcon);
  47. //可以传入type值,可自定义判断值
  48. if(titleBarType == 10){//不传入,默认为10,显示更多 文字,隐藏更多图标按钮
  49. ivMore.setVisibility(View.GONE);
  50. tvMore.setVisibility(View.VISIBLE);
  51. }else if(titleBarType == 11){//传入11,显示更多图标按钮,隐藏更多 文字
  52. tvMore.setVisibility(View.GONE);
  53. ivMore.setVisibility(View.VISIBLE);
  54. }
  55. }
  56. //左边图片点击事件
  57. public void setLeftIconOnClickListener(OnClickListener l){
  58. ivBack.setOnClickListener(l);
  59. }
  60. //右边图片点击事件
  61. public void setRightIconOnClickListener(OnClickListener l){
  62. ivBack.setOnClickListener(l);
  63. }
  64. //右边文字点击事件
  65. public void setRightTextOnClickListener(OnClickListener l){
  66. ivBack.setOnClickListener(l);
  67. }
  68. }

3.在res下的values下创建attr文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="CustomTitleBar">
  4. <attr name="title" format="string"/>
  5. <attr name="left_icon" format="reference"/>
  6. <attr name="right_icon" format="reference"/>
  7. <attr name="right_text" format="string"/>
  8. <attr name="titlebar_type" format="integer"/>
  9. </declare-styleable>
  10. </resources>

String是文字类型,references是图片类型,integer是数字类型 

4.需要用到我们的这个顶部标题栏的话,就在当前布局引入

可以根据type传入的值来改变右边显示文字还是图片,可在自定义View自定义该type值

  1. <com.titlebar.CustomTitleBar
  2. android:id="@+id/titlebar"
  3. android:background="#DCDCDC"
  4. app:right_icon="@drawable/icon_more"
  5. app:right_text="更多"
  6. app:titlebar_type="11"
  7. app:left_icon="@drawable/icon_back"
  8. app:title="我是标题"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"></com.titlebar.CustomTitleBar>

5.可以获取它的id,来调用它的点击事件

  1. CustomTitleBar titleBar = findViewById(R.id.titlebar);
  2. titleBar.setLeftIconOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. Toast.makeText(MainActivity.this, "左边", Toast.LENGTH_SHORT).show();
  6. }
  7. });

6.就这么多了,在这里贴上源码,小伙伴可以试试

Android灵活的自定义顶部标题栏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号