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

问题背景

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

问题分析

自定义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 RelativeLayout {
    boolean isLeft = true;
    public TimeClockView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        TextClock textClock = new TextClock(context);
        textClock.setId(View.generateViewId());
        textClock.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80);
        textClock.setFormat24Hour("hh:mm");
        textClock.setFormat12Hour("hh:mm");
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        this.addView(textClock, layoutParams);

        TextClock textClock1 = new TextClock(context);
        textClock1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
        textClock1.setFormat24Hour("MM月dd日 E");
        textClock1.setFormat12Hour("MM月dd日 E");
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        // 位于按钮一的下方
        layoutParams1.addRule(RelativeLayout.BELOW, textClock.getId());
        if (!isLeft) {
            layoutParams1.addRule(RelativeLayout.ALIGN_END, textClock.getId());
        }
        this.addView(textClock1, layoutParams1);
    }

    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"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns: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.TimeClockView
        android: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.TimeClockView
        android: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>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值