Android自定义TitleView标题开发实例

作者:_坚果 时间:2023-09-05 18:21:41 

Android开发过程中,经常遇到一个项目需要重复的定义相同样式的标题栏,Android相继推出了actionBar, toolBar, 相信有用到的朋友也会遇到一些不如意的时候,比如标题栏居中时,需要自定义xml文件给toolBar等,不了解actionBar,toolBar的可以去找相应的文章了解,这里介绍自定义titleBar满足国内主题风格样式的情况。

为了提前看到效果,先上效果图:

Android自定义TitleView标题开发实例

前期准备

1.为标题栏titleView预定义id,在values下的ids.xml中


<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tv_title_name" type="id"/>
<item name="tv_left_text" type="id"/>
<item name="iv_left_image" type="id"/>
<item name="iv_right_image" type="id"/>
<item name="iv_right_image_two" type="id"/>
<item name="tv_right_text" type="id"/>
<item name="tv_right_text_two" type="id"/>
</resources>

这里可以看到定义了左侧返回按钮id,标题id,后侧按钮id,左侧分两种情况:ImageView/TextView,右侧可以同时存在两个按钮,图片按钮、文字按钮组合。

2.自定义标题栏属性,在valuse下的attr.xml中


<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 标题属性 -->
<declare-styleable name="TitleAttr">
<attr name="title_name" format="reference|string"/>
<attr name="title_text_color" format="reference|color"/>
<attr name="title_drawable_right" format="reference"/>
<attr name="title_drawable_left" format="reference"/>
<attr name="title_text_size" format="reference|dimension"/>
<attr name="left_text" format="reference|string"/>
<attr name="left_image" format="reference"/>
<attr name="small_text_size" format="reference|dimension"/>
<attr name="title_gravity">
<enum name="left" value="3"/>
<enum name="center" value="17"/>
<enum name="right" value="5"/>
</attr>
<attr name="right_image" format="reference"/>
<attr name="right_text" format="reference|string"/>
<attr name="right_image_two" format="reference"/>
<attr name="right_text_two" format="reference|string"/>
<attr name="title_height" format="dimension"/>
<attr name="right_text_drawable_right" format="reference"/>
<attr name="right_text_drawable_left" format="reference"/>
<attr name="right_text_two_drawable_right" format="reference"/>
<attr name="right_text_two_drawable_left" format="reference"/>
<attr name="left_text_drawable_right" format="reference"/>
<attr name="left_text_drawable_left" format="reference"/>
</declare-styleable>
</resources>

•编码实现

• 有了前面的准备后,现在就可以开始编码实现了,这里先看看在xml中如何引入我们自定义的控件:


<com.jarek.library.TitleView
android:id="@+id/title_main"
android:layout_width="match_parent"
android:background="#0093fe"
title:title_name="标题"
title:right_text="@string/more"
title:title_text_color="@android:color/white"
title:right_image_two="@mipmap/icon_crop_rotate"
title:title_text_size="20sp"
title:small_text_size="15sp"
title:left_text="返回"
title:left_text_drawable_left="@mipmap/back_normal"
title:right_text_drawable_right="@mipmap/bar_button_right"
android:layout_height="50dp"/>

这里的title标签就是我们自定义,首先创建一个类继承自RelativeLayout,这里选择RelativeLayout作为父类容器,目的在于RelativeLayout便于控制相对位置。

首先我们要获得TypedArray对象,所有自定义属性的值通过它来获取:


TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleAttr);

得到了typedArray对象后,就可以开始添加按钮到容器中了,首先看看左边第一个返回按钮如果添加上去,先看代码:


int leftText = typeArray.getResourceId(R.styleable.TitleAttr_left_text, 0);
CharSequence charSequence = leftText > 0 ? typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_left_text);

这里left_text就是自定义属性,表明是左侧TextView显示的文字,文字可以是应用资源文件里的预定义string,也可以是直接输入文字,取到对应的styleable后,首先判断是否大于0,大于0表示是定义在string中的,通过typeArray.getResources().getText()获取值,等于0就直接取值,表示可能是通过直接赋值的方式给值的。取到值后怎么赋值给TextView,这里需要首先给他宽高,这是所有控件都需要的


/**
* layout params of RelativeLayout
* @return LayoutParams
*/
private LayoutParams initLayoutParams () {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
}

我们单独写一个方法,后续就可以直接通过


LayoutParams params = initLayoutParams();

来获取预设宽高值了,这里可以看到都是高度填充父控件,宽度是自适应。然后就是new一个TextView了:


/**
* create TextView
* @param context Context
* @param id the id of TextView
* @param charSequence text to show
* @param params relative params
* @return the TextView which is inited
*/
@NonNull
private TextView createTextView(Context context, int id, CharSequence charSequence, LayoutParams params) {
TextView textView = new TextView(context);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
textView.setId(id);
textView.setMinWidth((int)getPixelSizeByDp(minViewWidth));
textView.setText(charSequence);
return textView;
}

这里可以看到我们传入了预设的id值,需要显示的内容,以及上面给定的LayoutParams 。创建好TextView后还可以设置TextView的Drawable,通过自定义属性left_text_drawable_right,left_text_drawable_left设置,这里是设置了左右,上下对应的可以设置:


/**
* drawable of TextView
* @param typeArray TypedArray
* @param leftDrawableStyleable leftDrawableStyleable
* @param rightDrawableStyleable rightDrawableStyleable
* @param textView which TextView to set
*/
private void setTextViewDrawable(TypedArray typeArray, int leftDrawableStyleable, int rightDrawableStyleable, TextView textView) {
int leftDrawable = typeArray.getResourceId(leftDrawableStyleable, 0);
int rightDrawable = typeArray.getResourceId(rightDrawableStyleable, 0);
textView.setCompoundDrawablePadding((int)getPixelSizeByDp(drawablePadding));
textView.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, rightDrawable, 0);
}

这里也是抽取了一个方法出来,然后通过:


setTextViewDrawable(typeArray, R.styleable.TitleAttr_left_text_drawable_left, R.styleable.TitleAttr_left_text_drawable_right, mLeftBackTextTv);

即可给指定的TextView设置drawable了。创建好TextView后,前面提到我们用的是相对布局,需要指定位置规则:


params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

这里居左显示。同时还可以设置字体大小,通过自定义属性:small_text_size(两侧文字大小),title_text_size(标题文字大小)来设置字体:


/**
* get the dimension pixel size from typeArray which is defined in attr
* @param typeArray TypedArray
* @param stylable stylable
* @param defaultSize defaultSize
* @return the dimension pixel size
*/
private float getDimensionPixelSize(TypedArray typeArray, int stylable, int defaultSize) {
int sizeStyleable = typeArray.getResourceId(stylable, 0);
return sizeStyleable > 0 ? typeArray.getResources().getDimensionPixelSize(sizeStyleable) : typeArray.getDimensionPixelSize(stylable, (int)getPiselSizeBySp(defaultSize));
}

一样,这里也是单独写一个方法来做,TypedArray的用法就不多讲了,可以查看其它文章了解。然后通过如下设置字体大小:


float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_small_text_size, defaultSmallTextSize);
mLeftBackTextTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

文字颜色,同样的道理:


/**
* get textColor
* @param typeArray TypedArray
* @return textColor
*/
private int getTextColorFromAttr (TypedArray typeArray) {
int textColorStyleable = typeArray.getResourceId(R.styleable.TitleAttr_title_text_color, 0);
if (textColorStyleable > 0) {
return typeArray.getResources().getColor(textColorStyleable);
} else {
return typeArray.getColor(R.styleable.TitleAttr_title_text_color, textColor);
}
}

然后调用方法设置颜色:


mLeftBackTextTv.setTextColor(getTextColorFromAttr(typeArray));

到这里为止,左侧的第一个文字按钮。或者文字带图片的按钮就创建好了,最后就差一步了:


mLeftBackTextTv.setTextColor(getTextColorFromAttr(typeArray));

其它按钮,同样的道理,可以依次添加到容器中,就不多讲了,到此为止我们需要的titleView就创建好了,以后使用就可以直接调用了,不需要每个地方去重复的coding。

项目地址:github源码下载

以上所述是小编给大家介绍的Android自定义TitleView标题开发实例网站的支持!

来源:http://www.cnblogs.com/jarek/archive/2016/09/06/5846431.html

标签:android,titleview,标题
0
投稿

猜你喜欢

  • C++版本基于ros将文件夹中的图像转换为bag包

    2021-11-13 07:15:59
  • Spring如何消除代码中的if-else/switch-case

    2021-12-12 03:04:47
  • 基于OpenCv与JVM实现加载保存图像功能(JAVA 图像处理)

    2021-12-27 23:09:33
  • Ubuntu搭建Java开发环境笔记

    2023-10-10 14:27:49
  • while和for可以相互转换的例子分享

    2023-08-23 02:17:46
  • 如何使用BeanUtils.copyProperties进行对象之间的属性赋值

    2023-10-17 19:01:40
  • 基于Java实现获取本地IP地址和主机名

    2023-03-19 04:03:55
  • Java编程使用卡片布局管理器示例【基于swing组件】

    2022-03-03 10:46:33
  • 使用java基础类实现zip压缩和zip解压工具类分享

    2021-11-23 08:03:41
  • Java教程各种接口的介绍

    2022-04-29 08:49:25
  • Android蓝牙的开启和搜索设备功能开发实例

    2022-11-07 20:42:17
  • Android Studio实现简单的通讯录

    2023-06-13 06:49:56
  • Kotlin协程的启动方式介绍

    2022-05-26 10:29:27
  • C#程序加密工具.Net Reactor详细教程

    2021-07-27 18:08:39
  • SpringBoot如何用java生成静态html

    2023-08-08 08:46:59
  • C#中ref关键字的用法

    2022-07-17 21:30:47
  • Android提高之SQLite分页读取实现方法

    2022-11-28 22:44:04
  • Android自定义EditText右侧带图片控件

    2023-08-30 03:09:47
  • spring boot如何使用AOP统一处理web请求

    2023-05-16 14:15:11
  • Android实现双曲线折线图

    2023-07-29 15:07:28
  • asp之家 软件编程 m.aspxhome.com