欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义view实现TopBar效果

程序员文章站 2023-12-14 12:26:52
本文实例为大家分享了android自定义view实现topbar的具体代码,供大家参考,具体内容如下 布局文件

本文实例为大家分享了android自定义view实现topbar的具体代码,供大家参考,具体内容如下

布局文件

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/activity_main" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingbottom="@dimen/activity_vertical_margin" 
  android:paddingleft="@dimen/activity_horizontal_margin" 
  android:paddingright="@dimen/activity_horizontal_margin" 
  android:paddingtop="@dimen/activity_vertical_margin" 
  tools:context="com.bwie.test.mainactivity"> 
 
  <com.bwie.test.myview 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:lt="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/titlebar" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    lt:leftbuttontext="返回" 
    lt:leftbuttontextcolor="@android:color/white" 
    lt:leftbuttontextsize="8sp" 
    lt:buttonbgcolor="#4556ec" 
    lt:titletext="标题" 
    lt:titlecolor="@android:color/white" 
    lt:titlesize="8sp" 
    lt:rightbuttontext="完成" 
    lt:rightbuttontextcolor="@android:color/white" 
    lt:rightbuttontextsize="8sp" 
    android:background="#47ea10" 
    android:padding="10sp" 
    > 
  </com.bwie.test.myview> 
</relativelayout> 

自定义属性attrs.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <declare-styleable name="titlebar"> 
    <attr name="leftbuttontext" format="string|reference"></attr> 
    <attr name="leftbuttontextcolor" format="color|reference"></attr> 
    <attr name="leftbuttontextsize" format="dimension|reference"></attr> 
    <attr name="leftbuttonimage" format="color|reference"></attr> 
    <attr name="buttonbgcolor" format="color"/> 
 
    <attr name="titletext" format="string|reference"></attr> 
    <attr name="titlecolor" format="color|reference"></attr> 
    <attr name="titlesize" format="dimension|reference"></attr> 
 
    <attr name="rightbuttontext" format="string|reference"></attr> 
    <attr name="rightbuttontextcolor" format="color|reference"></attr> 
    <attr name="rightbuttontextsize" format="dimension|reference"></attr> 
    <attr name="rightbuttonimage" format="color|reference"></attr> 
 
 
  </declare-styleable> 
 
</resources> 

自定义view的class类

public class myview extends relativelayout{ 
 
  private string mleftbuttontext; 
  private int mleftbuttontextcolor; 
  private float mleftbuttonsize; 
  private drawable mleftbuttonimage; 
  private string mtitlebuttontext; 
  private int mtitlebuttontextcolor; 
  private float mtitlebuttonsize; 
  private string mrightbuttontext; 
  private int mrightbuttontextcolor; 
  private float mrightbuttonsize; 
  private drawable mrightbuttonimage; 
  private textview mrighttextview; 
  private textview titletextview; 
  private imageview mleftbutton; 
  private textview mlefttextview; 
  private imageview mrightbutton; 
  int buttonbgcolor; 
  public myview(context context) { 
    this(context,null); 
  } 
 
  public myview(context context, attributeset attrs) { 
    this(context, attrs,0); 
    typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.titlebar); 
    buttonbgcolor = typedarray.getcolor(r.styleable.titlebar_buttonbgcolor,color.blue); 
 
//左侧的按钮 
    mleftbuttontext = typedarray.getstring(r.styleable.titlebar_leftbuttontext); 
    mleftbuttontextcolor = typedarray.getcolor(r.styleable.titlebar_leftbuttontextcolor, color.gray); 
    mleftbuttonsize = typedarray.getdimension(r.styleable.titlebar_leftbuttontextsize, typedvalue.applydimension(typedvalue.complex_unit_sp, 16, getresources().getdisplaymetrics())); 
    mleftbuttonimage = typedarray.getdrawable(r.styleable.titlebar_leftbuttonimage); 
//中间的按钮 
    mtitlebuttontext = typedarray.getstring(r.styleable.titlebar_titletext); 
    mtitlebuttontextcolor = typedarray.getcolor(r.styleable.titlebar_titlecolor, color.gray); 
    mtitlebuttonsize = typedarray.getdimension(r.styleable.titlebar_titlesize, typedvalue.applydimension(typedvalue.complex_unit_sp, 16, getresources().getdisplaymetrics())); 
//右侧的按钮 
    mrightbuttontext = typedarray.getstring(r.styleable.titlebar_rightbuttontext); 
    mrightbuttontextcolor = typedarray.getcolor(r.styleable.titlebar_rightbuttontextcolor, color.gray); 
    mrightbuttonsize = typedarray.getdimension(r.styleable.titlebar_rightbuttontextsize, typedvalue.applydimension(typedvalue.complex_unit_sp, 16, getresources().getdisplaymetrics())); 
    mrightbuttonimage = typedarray.getdrawable(r.styleable.titlebar_rightbuttonimage); 
 
    typedarray.recycle();//回收 
    /*调用方法*/ 
    initview(context); 
  } 
 
  public myview(context context, attributeset attrs, int defstyleattr) { 
    this(context, attrs, defstyleattr,0); 
  } 
 
  public myview(context context, attributeset attrs, int defstyleattr, int defstyleres) { 
    super(context, attrs, defstyleattr, defstyleres); 
  } 
  /*构建按钮*/ 
  private void initview(context context) { 
 
    if(mleftbuttonimage == null & mleftbuttontext != null){ 
      // 当用户没有设置左侧按钮图片并设置了左侧的按钮文本属性时--添加左侧文本按钮 
      mlefttextview = new textview(context); 
      mlefttextview.settext(mleftbuttontext); 
      mlefttextview.settextcolor(mleftbuttontextcolor); 
      mlefttextview.settextsize(mleftbuttonsize); 
      mlefttextview.setbackgroundcolor(buttonbgcolor); 
      relativelayout.layoutparams leftparams = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); 
      leftparams.addrule(relativelayout.align_parent_left); 
      leftparams.addrule(relativelayout.center_vertical); 
      addview(mlefttextview, leftparams); 
    }else if(mleftbuttonimage != null){ 
      // 添加左侧图片按钮 
      relativelayout.layoutparams leftparams = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); 
      leftparams.addrule(relativelayout.align_parent_left); 
      leftparams.addrule(relativelayout.center_vertical); 
      mleftbutton = new imageview(context); 
      mleftbutton.setimagedrawable(mleftbuttonimage); 
      addview(mleftbutton, leftparams); 
    } 
 
    if(mtitlebuttontext!=null){ 
      // 添加中间标题 
      titletextview = new textview(context); 
      titletextview.settext(mtitlebuttontext); 
      titletextview.settextcolor(mtitlebuttontextcolor); 
      titletextview.settextsize(mtitlebuttonsize); 
      relativelayout.layoutparams titletextviewparams = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); 
      titletextviewparams.addrule(relativelayout.center_in_parent); 
      addview(titletextview,titletextviewparams); 
    } 
 
    if(mrightbuttonimage == null & mrightbuttontext != null){ 
      // 当用户没有设置右侧按钮图片并设置了左侧的按钮文本属性时--添加右侧文本按钮 
      mrighttextview = new textview(context); 
      mrighttextview.settext(mrightbuttontext); 
      mrighttextview.settextcolor(mrightbuttontextcolor); 
       mrighttextview.settextsize(mrightbuttonsize); 
      mrighttextview.setbackgroundcolor(buttonbgcolor); 
      relativelayout.layoutparams rightparams = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); 
      rightparams.addrule(relativelayout.align_parent_right); 
      rightparams.addrule(relativelayout.center_vertical); 
      addview(mrighttextview,rightparams); 
    }else if(mrightbuttonimage != null){ 
      // 添加右侧图片按钮 
      relativelayout.layoutparams rightparams = new relativelayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); 
      rightparams.addrule(relativelayout.align_parent_right); 
      rightparams.addrule(relativelayout.center_vertical); 
      mrightbutton = new imageview(context); 
      mrightbutton.setimagedrawable(mrightbuttonimage); 
      addview(mrightbutton, rightparams); 
    } 
  } 
  /*监听事件*/ 
  public interface onbuttonclicklistener{ 
    void onleftclick(); 
    void onrightclick(); 
  } 
  /*点击事件*/ 
  public void setonbuttonclicklistener(final onbuttonclicklistener onbuttonclicklistener) { 
    if(onbuttonclicklistener !=null){ 
      if(mlefttextview != null){ 
        mlefttextview.setonclicklistener(new onclicklistener() { 
          @override 
          public void onclick(view v) { 
            onbuttonclicklistener.onleftclick(); 
          } 
        }); 
      } 
      /*按钮*/ 
      if(mleftbutton != null){ 
        mleftbutton.setonclicklistener(new onclicklistener() { 
          @override 
          public void onclick(view v) { 
            onbuttonclicklistener.onleftclick(); 
          } 
        }); 
      } 
      if(mrighttextview != null){ 
        mrighttextview.setonclicklistener(new onclicklistener() { 
          @override 
          public void onclick(view v) { 
            onbuttonclicklistener.onrightclick(); 
          } 
        }); 
      } 
      /*按钮*/ 
      if(mrightbutton != null){ 
        mrightbutton.setonclicklistener(new onclicklistener() { 
          @override 
          public void onclick(view v) { 
            onbuttonclicklistener.onrightclick(); 
          } 
        }); 
      } 
    } 
  } 

main方法的代码调用自定义的类和点击事件

public class mainactivity extends appcompatactivity { 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
 
    /*找到控件*/ 
    myview myview = (myview) findviewbyid(r.id.titlebar); 
    /*点击事件*/ 
    myview.setonbuttonclicklistener(new myview.onbuttonclicklistener() { 
      @override 
      public void onleftclick() { 
        toast.maketext(mainactivity.this,"左侧按钮被点击了",toast.length_short).show(); 
      } 
 
      @override 
      public void onrightclick() { 
        toast.maketext(mainactivity.this,"右侧按钮被点击了",toast.length_short).show(); 
      } 
    }); 
 
  } 
} 

效果图:

Android 自定义view实现TopBar效果

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

上一篇:

下一篇: