全网整合营销服务商

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

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

Android 组合控件实现布局的复用的方法

看到很多项目会有实现自己的标题栏的做法,通常的界面是左边按钮或文字,加上中间的标题和右边的按钮或文字组成的。比较好的一种做法是使用include标签,复用同一个xml文件来实现布局的复用。但是这种方法是通过代码的方式来设置标题,左右按钮等其他的属性,会导致布局属性和Activity代码耦合性比较高。

因此,我们要通过自定义View,继承ViewGroup子类来实现这样的布局,降低布局文件和Activity代码耦合性。

首先,我们需要写出布局文件layout_custom_titlebar.xml。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 使用merge标签减少层级 -->
<Button
  android:id="@+id/title_bar_left"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_centerVertical="true"
  android:layout_marginLeft="5dp"
  android:background="@null"
  android:minHeight="45dp"
  android:minWidth="45dp"
  android:textSize="14sp" />

<TextView
  android:id="@+id/title_bar_title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:singleLine="true"
  android:textSize="17sp" />

<Button
  android:id="@+id/title_bar_right"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_centerVertical="true"
  android:layout_marginRight="7dp"
  android:background="@null"
  android:minHeight="45dp"
  android:minWidth="45dp"
  android:textSize="14sp" />

</merge>

2.定义自定义属性

<declare-styleable name="CustomTitleBar">
  <!--标题栏背景色-->
  <attr name="title_background_color" format="reference|integer" />
  <!--左边按钮是否可见-->
  <attr name="left_button_visible" format="boolean" />
  <!--右边按钮是否可见-->
  <attr name="right_button_visible" format="boolean" />
  <!--标题文字-->
  <attr name="title_text" format="string" />
  <!--标题文字颜色-->
  <attr name="title_text_color" format="color" />
  <!--标题文字图标-->
  <attr name="title_text_drawable" format="reference|integer" />
  <!--左边按钮文字-->
  <attr name="left_button_text" format="string" />
  <!--左边按钮文字颜色-->
  <attr name="left_button_text_color" format="color" />
  <!--左边按钮图标-->
  <attr name="left_button_drawable" format="reference|integer" />
  <!--右边按钮文字-->
  <attr name="right_button_text" format="string" />
  <!--右边按钮文字颜色-->
  <attr name="right_button_text_color" format="color" />
  <!--右边按钮图标-->
  <attr name="right_button_drawable" format="reference|integer" />
</declare-styleable>

3.自定义一个View继承ViewGroup子类,这里我们继承RelativeLayout。

public class CustomTitleBar extends RelativeLayout {
private Button titleBarLeftBtn;
private Button titleBarRightBtn;
private TextView titleBarTitle;

public CustomTitleBar(Context context) {
  super(context);
}

public CustomTitleBar(Context context, AttributeSet attrs) {
  super(context, attrs);

  LayoutInflater.from(context).inflate(R.layout.layout_custom_titlebar,this,true);
  titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
  titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
  titleBarTitle = (TextView) findViewById(R.id.title_bar_title);

  TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar);
  if(typedArray!=null){
    //titleBar背景色
    int titleBarBackGround=typedArray.getResourceId(R.styleable.CustomTitleBar_title_background_color, Color.BLUE);
    setBackgroundColor(titleBarBackGround);

    //获取是否要显示左边按钮
    boolean leftButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true);
    if (leftButtonVisible) {
      titleBarLeftBtn.setVisibility(View.VISIBLE);
    } else {
      titleBarLeftBtn.setVisibility(View.INVISIBLE);
    }
    //设置左边按钮的文字
    String leftButtonText = typedArray.getString(R.styleable.CustomTitleBar_left_button_text);
    if (!TextUtils.isEmpty(leftButtonText)) {
      titleBarLeftBtn.setText(leftButtonText);
      //设置左边按钮文字颜色
      int leftButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_left_button_text_color, Color.WHITE);
      titleBarLeftBtn.setTextColor(leftButtonTextColor);
    } else {
      //设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
      int leftButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.titlebar_back_icon);
      if (leftButtonDrawable != -1) {
        titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
      }
    }

    //先获取标题是否要显示图片icon
    int titleTextDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);
    if (titleTextDrawable != -1) {
      titleBarTitle.setBackgroundResource(titleTextDrawable);
    } else {
      //如果不是图片标题 则获取文字标题
      String titleText = typedArray.getString(R.styleable.CustomTitleBar_title_text);
      if (!TextUtils.isEmpty(titleText)) {
        titleBarTitle.setText(titleText);
      }
      //获取标题显示颜色
      int titleTextColor = typedArray.getColor(R.styleable.CustomTitleBar_title_text_color, Color.WHITE);
      titleBarTitle.setTextColor(titleTextColor);
    }

    //获取是否要显示右边按钮
    boolean rightButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true);
    if (rightButtonVisible) {
      titleBarRightBtn.setVisibility(View.VISIBLE);
    } else {
      titleBarRightBtn.setVisibility(View.INVISIBLE);
    }
    //设置右边按钮的文字
    String rightButtonText = typedArray.getString(R.styleable.CustomTitleBar_right_button_text);
    if (!TextUtils.isEmpty(rightButtonText)) {
      titleBarRightBtn.setText(rightButtonText);
      //设置右边按钮文字颜色
      int rightButtonTextColor = typedArray.getColor(R.styleable.CustomTitleBar_right_button_text_color, Color.BLUE);
      titleBarRightBtn.setTextColor(rightButtonTextColor);
    } else {
      //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
      int rightButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1);
      if (rightButtonDrawable != -1) {
        titleBarRightBtn.setBackgroundResource(rightButtonDrawable);
      }
    }
    typedArray.recycle();
  }

}

public void setTitleClickListener(OnClickListener onClickListener) {
  if (onClickListener != null) {
    titleBarLeftBtn.setOnClickListener(onClickListener);
    titleBarRightBtn.setOnClickListener(onClickListener);
  }
}

public Button getTitleBarLeftBtn() {
  return titleBarLeftBtn;
}

public Button getTitleBarRightBtn() {
  return titleBarRightBtn;
}

public TextView getTitleBarTitle() {
  return titleBarTitle;
}
}

4.正确地使用它

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mumubin.demoproject.view.CustomTitleBar
  android:id="@+id/ctb_view"
  android:layout_width="match_parent"
  android:layout_height="45dp"
  app:right_button_drawable="@mipmap/sure"
  app:title_text="@string/app_name" />

<com.mumubin.demoproject.view.CustomTitleBar
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_marginTop="4dp"
  app:title_background_color="@color/colorPrimary"
  app:title_text="@string/app_name"
  app:title_text_color="@color/colorAccent"
  app:left_button_text="左边"
  app:right_button_text="右边"/>

<com.mumubin.demoproject.view.CustomTitleBar
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_marginTop="4dp"
  app:title_text_drawable="@mipmap/ic_launcher"
  app:title_background_color="@color/colorAccent"
  app:left_button_text="左边"
  app:right_button_text="右边"/>
</LinearLayout>


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


# Android  # 布局复用  # 布局的复用  # Android动态添加设置布局与控件的方法  # Android开发之基本控件和四种布局方式详解  # Android 布局控件之LinearLayout详细介绍  # Android 图片网格布局控件示例代码  # Android布局控件之常用linearlayout布局  # Android布局优化之ViewStub控件  # Android编程布局控件之AbsoluteLayout用法实例分析  # Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)  # Android时间选择器、日期选择器实现代码  # Android开发实现布局中为控件添加选择器的方法  # 自定义  # 子类  # 来实现  # 自己的  # 背景色  # 复用  # 标题栏  # 会有  # 其他的  # 较高  # 比较好  # 如果不是  # 大家多多  # 正确地  # 这种方法  # 使用它  # singleLine  # layout_centerInParent  # title_bar_title  # declare 


相关文章: 如何在阿里云完成域名注册与建站?  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  红河网站制作公司,红河事业单位身份证如何上传?  购物网站制作公司有哪些,哪个购物网站比较好?  在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?  如何通过万网虚拟主机快速搭建网站?  哈尔滨网站建设策划,哈尔滨电工证查询网站?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  如何快速生成凡客建站的专业级图册?  如何制作算命网站,怎么注册算命网站?  岳西云建站教程与模板下载_一站式快速建站系统操作指南  招贴海报怎么做,什么是海报招贴?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  动图在线制作网站有哪些,滑动动图图集怎么做?  如何快速生成橙子建站落地页链接?  小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建  打鱼网站制作软件,波克捕鱼官方号怎么注册?  南平网站制作公司,2025年南平市事业单位报名时间?  seo网站制作优化,网站SEO优化步骤有哪些?  如何通过商城自助建站源码实现零基础高效建站?  香港服务器如何优化才能显著提升网站加载速度?  如何破解联通资金短缺导致的基站建设难题?  广州营销型建站服务商推荐:技术优势与SEO优化解析  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站主机选购指南与交易推荐:核心配置解析  如何在Windows 2008云服务器安全搭建网站?  如何正确选择百度移动适配建站域名?  如何通过WDCP绑定主域名及创建子域名站点?  赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?  建站VPS配置与SEO优化指南:关键词排名提升策略  宝盒自助建站智能生成技巧:SEO优化与关键词设置指南  网站制作公司排行榜,四大门户网站排名?  高性价比服务器租赁——企业级配置与24小时运维服务  头像制作网站在线制作软件,dw网页背景图像怎么设置?  如何在云主机上快速搭建多站点网站?  建站主机与虚拟主机有何区别?如何选择最优方案?  Thinkphp 中 distinct 的用法解析  如何将凡科建站内容保存为本地文件?  微信推文制作网站有哪些,怎么做微信推文,急?  上海网站制作网站建设公司,建筑电工证网上查询系统入口?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  浅谈Javascript中的Label语句  如何在IIS中新建站点并解决端口绑定冲突?  如何快速登录WAP自助建站平台?  网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?  如何在宝塔面板中修改默认建站目录?  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  建站org新手必看:2024最新搭建流程与模板选择技巧 

您的项目需求

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