Android 自定义view实现TopBar效果

所属分类: 软件编程 / Android 阅读数: 53
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多