Android 自定义view实现TopBar效果

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

本文实例为大家分享了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中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多