当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义简单的顶部标题栏

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

2019年08月01日  | 移动技术网IT编程  | 我要评论

ios应用推广,孤岛惊魂2汉化,志愿北京网站

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

实现功能:

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

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

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

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

老规矩,上几张效果图:

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

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

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

<?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文件

import android.content.context;
import android.content.res.typedarray;
import android.util.attributeset;
import android.view.layoutinflater;
import android.view.view;
import android.widget.imageview;
import android.widget.relativelayout;
import android.widget.textview;
 
/**
 * @author : 张
 * @email : manitozhang@foxmail.com
 * @date : 2018/9/19
 *
 * 一个简单的自定义标题栏
 */
 
public class customtitlebar extends relativelayout {
 
  private imageview ivback;
  private textview tvtitle;
  private textview tvmore;
  private imageview ivmore;
 
  public customtitlebar(context context, attributeset attrs) {
    super(context, attrs);
 
    initview(context,attrs);
  }
 
  //初始化视图
  private void initview(final context context, attributeset attributeset) {
    view inflate = layoutinflater.from(context).inflate(r.layout.layout_titlebar, this);
    ivback = inflate.findviewbyid(r.id.iv_back);
    tvtitle = inflate.findviewbyid(r.id.tv_title);
    tvmore = inflate.findviewbyid(r.id.tv_more);
    ivmore = inflate.findviewbyid(r.id.iv_more);
 
    init(context,attributeset);
  }
 
  //初始化资源文件
  public void init(context context, attributeset attributeset){
    typedarray typedarray = context.obtainstyledattributes(attributeset, r.styleable.customtitlebar);
    string title = typedarray.getstring(r.styleable.customtitlebar_title);//标题
    int lefticon = typedarray.getresourceid(r.styleable.customtitlebar_left_icon, r.drawable.icon_back);//左边图片
    int righticon = typedarray.getresourceid(r.styleable.customtitlebar_right_icon, r.drawable.icon_more);//右边图片
    string righttext = typedarray.getstring(r.styleable.customtitlebar_right_text);//右边文字
    int titlebartype = typedarray.getint(r.styleable.customtitlebar_titlebar_type, 10);//标题栏类型,默认为10
 
    //赋值进去我们的标题栏
    tvtitle.settext(title);
    ivback.setimageresource(lefticon);
    tvmore.settext(righttext);
    ivmore.setimageresource(righticon);
 
    //可以传入type值,可自定义判断值
    if(titlebartype == 10){//不传入,默认为10,显示更多 文字,隐藏更多图标按钮
      ivmore.setvisibility(view.gone);
      tvmore.setvisibility(view.visible);
    }else if(titlebartype == 11){//传入11,显示更多图标按钮,隐藏更多 文字
      tvmore.setvisibility(view.gone);
      ivmore.setvisibility(view.visible);
    }
  }
 
  //左边图片点击事件
  public void setlefticononclicklistener(onclicklistener l){
    ivback.setonclicklistener(l);
  }
 
  //右边图片点击事件
  public void setrighticononclicklistener(onclicklistener l){
    ivback.setonclicklistener(l);
  }
 
  //右边文字点击事件
  public void setrighttextonclicklistener(onclicklistener l){
    ivback.setonclicklistener(l);
  }
}

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

<?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值

<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,来调用它的点击事件

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.就这么多了,在这里贴上源码,小伙伴可以试试


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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网