第一种方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:background="#ff28f010"android:layout_height="100px"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ic_launcher"android:id="@+id/imageButton"android:gravity="center"android:text="返回"android:textColor="#f0a4cc"android:layout_margin="5dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="100px"android:id="@+id/textView2"android:text="标题"android:gravity="center_vertical|center_horizontal"android:textSize="45px"android:layout_weight="0.23" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageButton2"android:layout_margin="5dp"android:background="@drawable/ic_launcher"android:textColor="#f0a4cc"android:text="编辑"/></LinearLayout>

如果其他的地方引用,直接使用<include layout="@layout/title"/>

第二种方式:

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:background="#0000ff"android:layout_height="45dp"><Buttonandroid: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="@mipmap/ic_launcher"android:minHeight="45dp"android:minWidth="45dp"android:textSize="14sp" /><TextViewandroid:id="@+id/title_bar_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="登录"android:singleLine="true"android:textSize="17sp" /><Buttonandroid: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:text="提交"android:textColor="@android:color/white"android:background="@null"android:minHeight="45dp"android:minWidth="45dp"android:textSize="14sp" /></RelativeLayout></LinearLayout>

attrs。xml代码

<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="right_button_text" format="string" /><attr name="right_button_text_color" format="color" /><attr name="right_button_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" /></declare-styleable>

自定义:

public class CustomTitleBar  extends RelativeLayout {private Button titleBarLeftBtn;private Button titleBarRightBtn;private TextView titleBarTitle;public CustomTitleBar(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.custom_title_bar, 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 attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);if (attributes != null) {//处理titleBar背景色int titleBarBackGround = attributes.getResourceId(R.styleable.CustomTitleBar_title_background_color, Color.GREEN);setBackgroundResource(titleBarBackGround);//先处理左边按钮//获取是否要显示左边按钮boolean leftButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true);if (leftButtonVisible) {titleBarLeftBtn.setVisibility(View.VISIBLE);} else {titleBarLeftBtn.setVisibility(View.INVISIBLE);}//设置左边按钮的文字String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_left_button_text);if (!TextUtils.isEmpty(leftButtonText)) {titleBarLeftBtn.setText(leftButtonText);//设置左边按钮文字颜色int leftButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_left_button_text_color, Color.WHITE);titleBarLeftBtn.setTextColor(leftButtonTextColor);} else {//设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片int leftButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.ic_launcher);if (leftButtonDrawable != -1) {titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);}}//处理标题//先获取标题是否要显示图片iconint titleTextDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);if (titleTextDrawable != -1) {titleBarTitle.setBackgroundResource(titleTextDrawable);} else {//如果不是图片标题 则获取文字标题String titleText = attributes.getString(R.styleable.CustomTitleBar_title_text);if (!TextUtils.isEmpty(titleText)) {titleBarTitle.setText(titleText);}//获取标题显示颜色int titleTextColor = attributes.getColor(R.styleable.CustomTitleBar_title_text_color, Color.WHITE);titleBarTitle.setTextColor(titleTextColor);}//先处理右边按钮//获取是否要显示右边按钮boolean rightButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true);if (rightButtonVisible) {titleBarRightBtn.setVisibility(View.VISIBLE);} else {titleBarRightBtn.setVisibility(View.INVISIBLE);}//设置右边按钮的文字String rightButtonText = attributes.getString(R.styleable.CustomTitleBar_right_button_text);if (!TextUtils.isEmpty(rightButtonText)) {titleBarRightBtn.setText(rightButtonText);//设置右边按钮文字颜色int rightButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_right_button_text_color, Color.WHITE);titleBarRightBtn.setTextColor(rightButtonTextColor);} else {//设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片int rightButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1);if (rightButtonDrawable != -1) {titleBarRightBtn.setBackgroundResource(rightButtonDrawable);}}attributes.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;}}

最后其他布局使用

 <com.cqytjr.www.cheji.view.CustomTitleBarandroid:layout_width="match_parent"android:layout_height="45dp"android:layout_marginTop="10dp"tools:left_button_text="左边"tools:left_button_text_color="#ff0000"tools:right_button_drawable="@mipmap/titlebar_add_icon"tools:title_background_color="@color/blue"tools:title_text="标题5" />

转载于:https://www.cnblogs.com/hualuoshuijia/p/6895205.html

android studio 自定义控件相关推荐

  1. android自定义控件不显示,解决Android Studio Design界面不显示layout控件的问题

    Android Studio更新到3.1.3后,发现拖到Design中的控件在预览界面中不显示: 解决办法: 在Styles.xml中的parent="..."中的Theme前添加 ...

  2. android自定义控件 jar,Android Studio引用自定义的framework.jar包

    1.在app/libs/目录下添加framework.jar包. 2.打开build->Edit Libraries and Dependencies,把libs/framework.jar放到 ...

  3. 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能

    以往的Android开发有一个头疼的且拖慢速度的问题,就是你每改一行代码要想看到结果必须要编译运行到手机或者模拟器上,而且需要从头(可能是登录界面)一直点击到你修改的界面为止.开发一个完整的Andro ...

  4. 在Android Studio中进行代码混淆

    此文章来源于http://blog.csdn.net/wenwen091100304/article/details/52802247点击打开链接 在Android APP开发完成后,为了防止APP被 ...

  5. Android Studio 代码混淆(你真的会混淆吗)

    一.前言 今天要打包新产品,突然忘了混淆的参数是怎么写的了,虽然之前也混淆过,可是具体配置的参数代码有些记不起来了,因此决定花点时间写篇博客记录一下,方便以后查找和自己的记忆. 二.Android S ...

  6. Android(一)——Eclipse+ADT 和 Android Studio 开发环境搭建及介绍

    文章目录 1. 前言介绍 1.1 JDK安装与配置 1.2 IDE开发APP流程 1.3 相关术语的解析 1.4 ADB相关指令 1.5 APP程序打包与安装流程 2. 开发环境搭建 2.1 Ecli ...

  7. Android Studio 代码混淆

    2019独角兽企业重金招聘Python工程师标准>>> 在Android studio 进行代码混淆配置. proguard 配置 -keepclasseswithmembers 指 ...

  8. Android Studio(16)---工程相关解析(各种文件,资源访问)

    工程相关解析(各种文件,资源访问) 前面讲了一堆看似和我们Android开发无关的东西是吧,当然是现在看似而已,以后你回头看就知道了! 好吧,本节我们就来以前面创建的Hello World项目为入口, ...

  9. (转)Android studio 使用心得(五)—代码混淆和破解apk

    这篇文章等是跟大家分享一在Android studio 进行代码混淆配置.之前大家在eclipse上也弄过代码混淆配置,其实一样,大家可以把之前在eclipse上的配置文件直接拿过来用.不管是.cfg ...

最新文章

  1. 大写的服,看完这篇你还不懂RocketMQ算我输
  2. C 语言编程 — 高级数据类型 — 共用体
  3. php和python交互-浅析PHP与Python进行数据交互
  4. 用CSS控制表格的框格线
  5. CITRIX 5.0,XML服务异常引出事件ID 31003和30016错误
  6. python读取栅格gdal库下载链接
  7. Docker系列之一:入门介绍
  8. C#比较两个日期的大小两种案例解析
  9. 作战手册-2011-12-18
  10. YOLO系列:YOLO v2深度解析 v1 vs v2
  11. 完全卸载oracle11g
  12. python zip函数小结
  13. OFDM中的DC subcarrier
  14. 乌拉、利用python实现tree命令
  15. COMSOL:案列应用实操教学---光电
  16. 【围棋棋盘绘制——html实现】
  17. 名悦集团:深圳人买什么车好?
  18. bzoj4516 [Sdoi2016]生成魔咒
  19. android 局域网socket,Android基于局域网socket通信
  20. html tbody增加行,使用jquery向表的tbody添加行

热门文章

  1. osgEarth示例分析——osgearth_minimap
  2. 腾讯云多人视频会议 TUIRoom 功能体验
  3. CRM软件哪个好?该如何选择?
  4. 2013年8月28日、PS初步入门|and|Maven了解
  5. kaggle竞赛 | 计算机视觉 | Doodle Recognition Challenge
  6. WHUT·PTA·实验一 (个人整理+理解版 )
  7. js获取当前时间、获取未来多少天的时间、获取星期、获取某一天的年月方法
  8. 【十三届蓝桥杯】2022年蓝桥杯省赛个人解答 C++ B 组
  9. 庞加莱 (Jules Henri Poincaré)
  10. 60个有用的css代码片段