安卓开发自定义时间日期显示组件
问题背景
实现时间和日期显示,左对齐和对齐两种效果,如下图所示:
问题分析
自定义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) {