当前位置: 首页 > news >正文

安卓开发自定义时间日期显示组件

安卓开发自定义时间日期显示组件

问题背景

实现时间和日期显示,左对齐和对齐两种效果,如下图所示:
在这里插入图片描述

问题分析

自定义view实现一般思路:
(1)自定义一个View
(2)编写values/attrs.xml,在其中编写styleable和item等标签元素
(3)在布局文件中View使用自定义的属性
(4)在View的构造方法中通过TypedArray获取

问题解决

话不多说,直接上代码
(1)编写values/attrs.xml,组件定义left属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="TimeClockView"><attr name="left" format="boolean"/></declare-styleable>
</resources>

(2)自定义View,代码如下:

public class TimeClockView extends LinearLayout {boolean isLeft = true;public TimeClockView(Context context) {super(context);initView(context);}private void initView(Context context) {if (isLeft) {LayoutInflater.from(context).inflate(R.layout.layout_time_date,this);} else {LayoutInflater.from(context).inflate(R.layout.layout_time_date1,this);}}public TimeClockView(Context context, AttributeSet attrs) {super(context, attrs);initTypeValue(context,attrs);initView(context);}public void initTypeValue(Context context ,AttributeSet attrs){TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeClockView);isLeft = a.getBoolean(R.styleable.TimeClockView_left, true);a.recycle();}
}

(3)自定义view对应的布局文件如下:
左对齐:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextClockandroid:id="@+id/time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:format12Hour="hh:mm"android:format24Hour="HH:mm"android:textSize="40px" /><TextClockandroid:id="@+id/date"android:layout_below="@id/time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:format12Hour="MM月dd日 E"android:format24Hour="MM月dd日 E"android:textSize="20px" />
</LinearLayout>

右对齐:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:gravity="end"android:orientation="vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextClockandroid:id="@+id/time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:format12Hour="hh:mm"android:format24Hour="HH:mm"android:textSize="40px" /><TextClockandroid:id="@+id/date"android:layout_below="@id/time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:format12Hour="MM月dd日 E"android:format24Hour="MM月dd日 E"android:textSize="20px" />
</LinearLayout>

(4)在页面布局中,使用自定义的view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.baorant.mytestnew.view.TimeClockViewandroid:layout_marginLeft="90px"android:layout_marginTop="70px"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"android:layout_width="wrap_content"android:layout_height="wrap_content" /><com.baorant.mytestnew.view.TimeClockViewandroid:layout_marginRight="90px"android:layout_marginTop="70px"app:left="false"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"android:layout_width="wrap_content"android:layout_height="wrap_content"/></androidx.constraintlayout.widget.ConstraintLayout>
http://www.lryc.cn/news/389924.html

相关文章:

  • IT行业入门,如何假期逆袭,实现抢跑
  • Pyramid 中混合认证策略
  • 深度学习经典检测方法概述
  • <sa8650>sa8650 qcxserver-之-摄像头传感器VB56G4A驱动开发<1>
  • 推荐8款超实用的ComfyUI绘画插件,帮助我们的AI绘画质量和效率提升几个档次!
  • MATLAB-振动问题:两自由度耦合系统自由振动
  • 人工智能-NLP简单知识汇总01
  • Spring Boot中的异步编程技巧
  • 深度解密Spark性能优化之道
  • 在U盘/移动硬盘上安装热插拔式Ubuntu系统,并将Docker目录挂载到NTFS硬盘
  • 商城小程序论文(设计)开题报告
  • 15. Java的 CAS 操作原理
  • 修改element-ui日期下拉框datetimePicker的背景色样式
  • Linux—— 逻辑运算符,压缩和解压缩
  • 音视频入门基础:H.264专题(6)——FFmpeg源码:从H.264码流中提取NALU Header、EBSP、RBSP和SODB
  • STM32实现按键单击、双击、长按、连按功能,使用状态机,无延时,不阻塞
  • C#之Delta并联机械手的视觉同步分拣
  • 01:Linux的基本命令
  • GNSS 载波、测距码和导航电文的关系简介
  • deepE 定位系统卡顿问题实战(一) ----------- 锁造成的阻塞问题
  • YOLOv5改进 | 主干网络 | ODConv + ConvNeXt 增强目标特征提取能力
  • TIA博途WinCC通过VB脚本从 Excel中读取数据的具体方法介绍
  • 第5篇 区块链的技术架构:节点、网络和数据结构
  • vue长列表,虚拟滚动
  • 【实战场景】记一次UAT jvm故障排查经历
  • 线性代数--行列式1
  • tensorflow神经网络
  • Python基础001
  • 【udp报文】udp报文未自动分片,报文过长被拦截问题定位
  • 某网页gpt的JS逆向