全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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  # android之listview悬浮topBar效果  # 自定义  # 大家分享  # 具体内容  # 大家多多  # background  # rightButtonTextSize  # rightButtonText  # rightButtonTextColor  # resources  # declare  # attrs  # padding  # titleSize  # white  # leftButtonTextSize  # color  # leftButtonText  # leftButtonTextColor  # sp  # titleText 


相关文章: 如何在万网开始建站?分步指南解析  广州商城建站系统开发成本与周期如何控制?  宁波免费建站如何选择可靠模板与平台?  香港服务器部署网站为何提示未备案?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  制作网页的网站有哪些,电脑上怎么做网页?  如何用狗爹虚拟主机快速搭建网站?  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  外贸公司网站制作哪家好,maersk船公司官网?  ,柠檬视频怎样兑换vip?  香港服务器网站生成指南:免费资源整合与高速稳定配置方案  如何在建站之星绑定自定义域名?  湖北网站制作公司有哪些,湖北清能集团官网?  专业公司网站制作公司,用什么语言做企业网站比较好?  韩国服务器如何优化跨境访问实现高效连接?  ui设计制作网站有哪些,手机UI设计网址吗?  如何配置支付宝与微信支付功能?  如何正确选择百度移动适配建站域名?  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  制作证书网站有哪些,全国城建培训中心证书查询官网?  番禺网站制作公司哪家值得合作,番禺图书馆新馆开放了吗?  文字头像制作网站推荐软件,醒图能自动配文字吗?  如何获取开源自助建站系统免费下载链接?  如何选择高效可靠的多用户建站源码资源?  XML的“混合内容”是什么 怎么用DTD或XSD定义  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  建站主机选哪种环境更利于SEO优化?  如何用西部建站助手快速创建专业网站?  深圳网站制作的公司有哪些,dido官方网站?  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  开心动漫网站制作软件下载,十分开心动画为何停播?  香港服务器选型指南:免备案配置与高效建站方案解析  建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南  正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?  寿县云建站:智能SEO优化与多行业模板快速上线指南  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  Python如何创建带属性的XML节点  如何通过西部建站助手安装IIS服务器?  ,购物网站怎么盈利呢?  建站主机SSH密钥生成步骤及常见问题解答?  C++时间戳转换成日期时间的步骤和示例代码  如何选择网络建站服务器?高效建站必看指南  Java解压缩zip - 解压缩多个文件或文件夹实例  安徽网站建设与外贸建站服务专业定制方案  如何选择适合PHP云建站的开源框架?  如何选择适配移动端的WAP自助建站平台?  h5网站制作工具有哪些,h5页面制作工具有哪些?  公司网站的制作公司,企业网站制作基本流程有哪些? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。