广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义控件如何在XML文件中使用自定义属性
  • 676
分享到

Android自定义控件如何在XML文件中使用自定义属性

Android XML自定义属性Android 自定义控件 2023-05-14 20:05:17 676人浏览 八月长安
摘要

目录前言一、为什么需要自定义控件二、具体步骤1.首先我们创建一个 layout xml文件:2.为自定义控件创建java类:3.在res/values下,新建一个attrs.xml文

前言

你好, 我是Cici。这几天在做一个小项目的时候,用到了自定义控件,为了方便在XML中进行配置,于是需要用到自定义属性,特此记下用法,方便复习的同时也希望对大家有所帮助。

一、为什么需要自定义控件

Android本身提供了很多控件,比如TextView、ImageView等,在实际开发中,有时候单个的控件并不能很好的满足业务需求,因此我们会将多种控件组合在一起,形成一个具有特定功能的自定义控件,就好比零件的拼装,将多个小零件最后拼成一个大零件来使用。

二、具体步骤

1.首先我们创建一个 layout xml文件:

例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="Http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mi_layout">
    <View
        android:id="@+id/view_1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/line"
        app:layout_constraintTop_toTopOf="parent" />
    <ImageView
        android:id="@+id/image_1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@color/blue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="10dp" />
    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/image_1"
        app:layout_constraintStart_toEndOf="@id/image_1"
        android:layout_marginStart="8dp"
        android:text="预约申请"
        android:textSize="14sp"
        android:textColor="@color/black" />
    <TextView
        android:id="@+id/text_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@id/text_1"
        app:layout_constraintTop_toBottomOf="@id/text_1"
        app:layout_constraintBottom_toBottomOf="@id/image_1"
        app:layout_constraintEnd_toStartOf="@id/image_2"
        android:layout_marginEnd="66dp"
        android:layout_marginTop="10dp"
        android:ellipsize="end"
        android:lines="1"
        android:text="李老师申请实验室"
        android:textSize="12sp"
        android:textColor="@color/gray" />
    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/image_2"
        android:layout_width="8dp"
        android:layout_height="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="20dp"
        app:roundPercent="1"
        android:background="@color/color_f31515" />
    <View
        android:id="@+id/view_2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/line"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

具体效果如下图所示

2.为自定义控件创建java类:

public class MessageItem extends ConstraintLayout {
    private LayoutMessageItemBinding binding;
    ConstraintLayout layout;
    ImageView iv1;
    TextView tv1;
    TextView tv2;
    ImageView iv2;
    private int imageResource1 = R.color.bottom_down;
    private int imageResource2 = R.color.color_f31515;
    private String text1 = "";
    private String text2 = "";
    private boolean showSpot = true;
    public MessageItem(@NonNull Context context) {
        this(context, null);
    }
    public MessageItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MessageItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MessageItem);
        imageResource1 = array.getResourceId(R.styleable.MessageItem_imageResource1, imageResource1);
        imageResource2 = array.getResourceId(R.styleable.MessageItem_imageResource2, imageResource2);
        text1 = array.getString(R.styleable.MessageItem_text1);
        text2 = array.getString(R.styleable.MessageItem_text2);
        showSpot = array.getBoolean(R.styleable.MessageItem_showSpot, true);
        array.recycle();
        if (isInEditMode()) {
            return;
        }
        binding = LayoutMessageItemBinding.inflate(LayoutInflater.from(MyApplication.getContext()), this, true);
        bindView();
        init();
    }
    private void bindView() {
        layout = binding.miLayout;
        iv1 = binding.image1;
        iv2 = binding.image2;
        tv1 = binding.text1;
        tv2 = binding.text2;
    }
    private void init() {
        setImageResource1(imageResource1);
        setImageResource2(imageResource2);
        setText1(text1);
        setText2(text2);
        setShowSpot(showSpot);
    }
    private MessageItem setImageResource1(int resId) {
        this.iv1.setBackgroundResource(resId);
        return this;
    }
    private MessageItem setImageResource2(int resId) {
        this.iv2.setBackgroundResource(resId);
        return this;
    }
    private MessageItem setText1(String text) {
        this.tv1.setText(text);
        return this;
    }
    private MessageItem setText2(String text) {
        this.tv2.setText(text);
        return this;
    }
    private MessageItem setShowSpot(boolean b) {
        this.iv1.setVisibility(b ? VISIBLE : GoNE);
        return this;
    }
}

3.在res/values下,新建一个attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MessageItem">
        <attr name="imageResource1" fORMat="reference" />
        <attr name="imageResource2" format="reference" />
        <attr name="text1" format="string" />
        <attr name="text2" format="string" />
        <attr name="showSpot" format="boolean" />
    </declare-styleable>
</resources>

4.最后使用:

在使用的时候,我们只需要在相应的 XML 文件中引入即可,例如:

<com.example.lims.widget.MessageItem
    android:id="@+id/f_message_i3"
    app:text1="维修意见"
    app:text2="张同学向您提出设备维护意见"
    app:imageResource1="@drawable/message_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/f_message_i2"
    app:layout_constraintStart_toStartOf="parent" />
<com.example.lims.widget.MessageItem
    android:id="@+id/f_message_i1"
    app:text1="预约申请"
    app:text2="王老师向您申请实验室"
    app:imageResource1="@drawable/message_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintStart_toStartOf="parent" />

其中,app:text1、app:text2、app:imageResource1 为自定义属性。这样就可以根据业务需要,为我们的自定义控件设置不同的属性值,最终得到不同的效果。

以上就是Android自定义控件--如何在XML文件中使用自定义属性的详细内容,更多关于Android XML自定义属性的资料请关注编程网其它相关文章!

--结束END--

本文标题: Android自定义控件如何在XML文件中使用自定义属性

本文链接: https://www.lsjlt.com/news/204507.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android自定义控件如何在XML文件中使用自定义属性
    目录前言一、为什么需要自定义控件二、具体步骤1.首先我们创建一个 layout xml文件:2.为自定义控件创建java类:3.在res/values下,新建一个attrs.xml文...
    99+
    2023-05-14
    Android XML自定义属性 Android 自定义控件
  • Android自定义控件之自定义属性(二)
    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性。本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解。有关原理知识请参...
    99+
    2022-06-06
    属性 自定义属性 Android
  • Android怎么在XML文件中自定义控件
    今天小编给大家分享一下Android怎么在XML文件中自定义控件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、为什么需要...
    99+
    2023-07-05
  • android 自定义控件 自定义属性详细介绍
    自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地...
    99+
    2022-06-06
    自定义 自定义控件 属性 自定义属性 Android
  • Android自定义组件:2、如何实现和使用自定义组件、自定义属性
    声明:本教程不收取任何费用,欢迎转载,尊重作者劳动成果,不得用于商业用途,侵权必究!!! 目录 一、前言 二、如何实现自定义组件 步骤1:写 attrs.xml 资源文件 1、...
    99+
    2022-06-06
    属性 自定义属性 Android
  • 详解Android自定义控件属性
    在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢? 在文件res/values/下新建...
    99+
    2022-06-06
    属性 Android
  • wpf如何自定义控件属性
    在WPF中,可以通过创建自定义控件继承自现有的控件,并添加自定义属性来实现自定义控件属性。 以下是一个简单的示例,展示了如何创建一个...
    99+
    2023-10-24
    wpf
  • Android系统控件获取自定义属性
    我们如果想在ImageView,Button,TextView等系统控件中在XML中配置自定义属性该如何实现呢?例如我们有一个scrollView,在ScrollView里面有...
    99+
    2022-06-06
    自定义 属性 自定义属性 Android
  • Android自定义控件属性详细介绍
     Android自定义控件属性详细介绍1. reference:参考某一资源ID。     (1)属性定义: <declare-styleable name = "名称"> ...
    99+
    2023-05-31
    android 自定义 控件
  • android 自定义控件 使用declare
    在Android中,可以使用`declare-styleable`来定义和使用自定义控件的属性。下面是一个简单的示例:1. 在res...
    99+
    2023-09-21
    Android
  • android自定义控件使用declare-styleable进行配置属性
    在Android中,可以使用declare-styleable来定义和配置自定义控件的属性。下面是一个简单的示例:首先,在res/v...
    99+
    2023-09-23
    Android
  • 详解Android自定义控件属性TypedArray以及attrs
    最近在研究android自定义控件属性,学到了TypedArray以及attrs。大家也可以结合《理解Android中的自定义属性》这篇文章进行学习,后续一篇还有应用。 1、a...
    99+
    2022-06-06
    Android
  • Android中自定义控件的declare-styleable属性重用方案
    在Android中,我们可以通过自定义控件的declare-styleable属性来实现样式的重用。declare-styleabl...
    99+
    2023-08-11
    Android
  • 如何在Android应用中自定义一个控件
    本篇文章为大家展示了如何在Android应用中自定义一个控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。开发自定义控件的步骤:了解View的工作原理 2、 编写继承自View的子类3、 为自定义V...
    99+
    2023-05-31
    android roi
  • Android如何自定义评分控件
    今天小编给大家分享一下Android如何自定义评分控件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。自定义参数为了方便扩展,...
    99+
    2023-06-30
  • 浅析Android手机卫士自定义控件的属性
    推荐阅读:浅析Android手机卫士关闭自动更新 上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性 上一节组合控件SettingIte...
    99+
    2022-06-06
    自定义 自定义控件 属性 Android
  • 怎么在Android中自定义一个控件
    怎么在Android中自定义一个控件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。代码class SleepDayChart(context: Contex...
    99+
    2023-06-14
  • Android如何自定义View歌词控件
    本篇内容介绍了“Android如何自定义View歌词控件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!目录前言一、 歌词解析歌词实体类Lrc...
    99+
    2023-06-20
  • 如何在Android中自定义UI组件
    如何在Android中自定义UI组件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Android开发自定义UI组件实现红色小球跟随手指移动要写实现自定义UI组件,要创建一个Ba...
    99+
    2023-06-15
  • 如何在css中自定义属性
    本篇文章给大家分享的是有关如何在css中自定义属性,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1* css变量的语法<1> 什么是css变量?如果您使用过任何一种...
    99+
    2023-06-08
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作