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

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

问题背景

实现时间和日期显示,左对齐和对齐两种效果,如下图所示:

问题分析

自定义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">

    <TextClock
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="hh:mm"
        android:format24Hour="HH:mm"
        android:textSize="40px" />

    <TextClock
        android: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">

    <TextClock
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="hh:mm"
        android:format24Hour="HH:mm"
        android:textSize="40px" />

    <TextClock
        android: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.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>
相关推荐
雨白13 分钟前
Kotlin 协程的灵魂:结构化并发详解
android·kotlin
我命由我1234518 分钟前
Android 开发问题:getLeft、getRight、getTop、getBottom 方法返回的值都为 0
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Modu_MrLiu19 分钟前
Android实战进阶 - 用户闲置超时自动退出登录功能详解
android·超时保护·实战进阶·长时间未操作超时保护·闲置超时
Jeled40 分钟前
Android 网络层最佳实践:Retrofit + OkHttp 封装与实战
android·okhttp·kotlin·android studio·retrofit
信田君952741 分钟前
瑞莎星瑞(Radxa Orion O6) 基于 Android OS 使用 NPU的图片模糊查找APP 开发
android·人工智能·深度学习·神经网络
tangweiguo030519871 小时前
Kotlin 实现 Android 网络状态检测工具类
android·网络·kotlin
nvvas2 小时前
Android Studio JAVA开发按钮跳转功能
android·java·android studio
怪兽20142 小时前
Android多进程通信机制
android·面试
叶羽西3 小时前
Android CarService调试操作
android
千里马-horse3 小时前
在android中 spdlog库的log如何在控制台上输出
android·c++·spdlog