服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义简单的顶部标题栏

Android自定义简单的顶部标题栏

2022-08-24 14:19zpf_ Android

这篇文章主要为大家详细介绍了Android自定义简单的顶部标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

实现功能:

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

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

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

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

老规矩,上几张效果图:

Android自定义简单的顶部标题栏

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="50dp">
 
  <ImageView
    android:id="@+id/iv_back"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="20dp"
    android:src="@drawable/icon_back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="标题"
    android:textColor="#000"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <TextView
    android:id="@+id/tv_more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="更多"
    android:textColor="#000"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  <ImageView
    android:id="@+id/iv_more"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/icon_more"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
</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. /** 
  11.  * @Author : 张 
  12.  * @Email : manitozhang@foxmail.com 
  13.  * @Date : 2018/9/19 
  14.  * 
  15.  * 一个简单的自定义标题栏 
  16.  */ 
  17.   
  18. public class CustomTitleBar extends RelativeLayout { 
  19.   
  20.   private ImageView ivBack; 
  21.   private TextView tvTitle; 
  22.   private TextView tvMore; 
  23.   private ImageView ivMore; 
  24.   
  25.   public CustomTitleBar(Context context, AttributeSet attrs) { 
  26.     super(context, attrs); 
  27.   
  28.     initView(context,attrs); 
  29.   } 
  30.   
  31.   //初始化视图 
  32.   private void initView(final Context context, AttributeSet attributeSet) { 
  33.     View inflate = LayoutInflater.from(context).inflate(R.layout.layout_titlebar, this); 
  34.     ivBack = inflate.findViewById(R.id.iv_back); 
  35.     tvTitle = inflate.findViewById(R.id.tv_title); 
  36.     tvMore = inflate.findViewById(R.id.tv_more); 
  37.     ivMore = inflate.findViewById(R.id.iv_more); 
  38.   
  39.     init(context,attributeSet); 
  40.   } 
  41.   
  42.   //初始化资源文件 
  43.   public void init(Context context, AttributeSet attributeSet){ 
  44.     TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomTitleBar); 
  45.     String title = typedArray.getString(R.styleable.CustomTitleBar_title);//标题 
  46.     int leftIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_left_icon, R.drawable.icon_back);//左边图片 
  47.     int rightIcon = typedArray.getResourceId(R.styleable.CustomTitleBar_right_icon, R.drawable.icon_more);//右边图片 
  48.     String rightText = typedArray.getString(R.styleable.CustomTitleBar_right_text);//右边文字 
  49.     int titleBarType = typedArray.getInt(R.styleable.CustomTitleBar_titlebar_type, 10);//标题栏类型,默认为10 
  50.   
  51.     //赋值进去我们的标题栏 
  52.     tvTitle.setText(title); 
  53.     ivBack.setImageResource(leftIcon); 
  54.     tvMore.setText(rightText); 
  55.     ivMore.setImageResource(rightIcon); 
  56.   
  57.     //可以传入type值,可自定义判断值 
  58.     if(titleBarType == 10){//不传入,默认为10,显示更多 文字,隐藏更多图标按钮 
  59.       ivMore.setVisibility(View.GONE); 
  60.       tvMore.setVisibility(View.VISIBLE); 
  61.     }else if(titleBarType == 11){//传入11,显示更多图标按钮,隐藏更多 文字 
  62.       tvMore.setVisibility(View.GONE); 
  63.       ivMore.setVisibility(View.VISIBLE); 
  64.     } 
  65.   } 
  66.   
  67.   //左边图片点击事件 
  68.   public void setLeftIconOnClickListener(OnClickListener l){ 
  69.     ivBack.setOnClickListener(l); 
  70.   } 
  71.   
  72.   //右边图片点击事件 
  73.   public void setRightIconOnClickListener(OnClickListener l){ 
  74.     ivBack.setOnClickListener(l); 
  75.   } 
  76.   
  77.   //右边文字点击事件 
  78.   public void setRightTextOnClickListener(OnClickListener l){ 
  79.     ivBack.setOnClickListener(l); 
  80.   } 

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

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

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

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

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

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

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

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

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

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

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

原文链接:https://blog.csdn.net/qq1271396448/article/details/82774433

延伸 · 阅读

精彩推荐