广告
返回顶部
首页 > 资讯 > 精选 >Android怎么在XML文件中自定义控件
  • 715
分享到

Android怎么在XML文件中自定义控件

2023-07-05 22:07:56 715人浏览 泡泡鱼
摘要

今天小编给大家分享一下Android怎么在XML文件中自定义控件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、为什么需要

今天小编给大家分享一下Android怎么在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>

具体效果如下图所示

Android怎么在XML文件中自定义控件

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文件中自定义控件”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。

--结束END--

本文标题: Android怎么在XML文件中自定义控件

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

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

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

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

下载Word文档
猜你喜欢
  • Android怎么在XML文件中自定义控件
    今天小编给大家分享一下Android怎么在XML文件中自定义控件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、为什么需要...
    99+
    2023-07-05
  • Android自定义控件如何在XML文件中使用自定义属性
    目录前言一、为什么需要自定义控件二、具体步骤1.首先我们创建一个 layout xml文件:2.为自定义控件创建java类:3.在res/values下,新建一个attrs.xml文...
    99+
    2023-05-14
    Android XML自定义属性 Android 自定义控件
  • 怎么在Android中自定义一个控件
    怎么在Android中自定义一个控件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。代码class SleepDayChart(context: Contex...
    99+
    2023-06-14
  • Android中怎么自定义Progress控件
    Android中怎么自定义Progress控件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。主要就是需求就是椭圆进度,百分比跟随渐变背景,这样一想其实就是一个布局,然后控制...
    99+
    2023-05-31
    android progress
  • 怎么在Android中自定义一个ProgressBar控件
    这篇文章将为大家详细讲解有关怎么在Android中自定义一个ProgressBar控件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。首先加载Drawable,在onMeasure设置好其区域...
    99+
    2023-05-30
    android progressbar
  • Android中怎么自定义选择控件
    本篇文章为大家展示了Android中怎么自定义选择控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、自定义DialogDialog布局文件<xml version="1...
    99+
    2023-05-30
    android
  • Android自定义控件之自定义组合控件(三)
    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件基本原理详解(一)、Android自定义控件之自定义属性(二)。今天重点介绍一下如何通过自定义组合控件来提高布...
    99+
    2022-06-06
    Android
  • 怎么在Android中实现一个自定义控件
    今天就跟大家聊聊有关怎么在Android中实现一个自定义控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。首先定义一个layout实现按钮内部布局:<xml vers...
    99+
    2023-05-31
    android
  • android怎么自定义组合控件
    要自定义一个组合控件,你可以按照以下步骤进行:1. 创建一个新的类,继承自现有的Android控件类,例如LinearLayout或...
    99+
    2023-08-09
    android
  • android怎么自定义开关控件
    要自定义开关控件,可以使用以下步骤:1. 创建一个自定义的开关控件类,继承自Switch或CompoundButton类。2. 在自...
    99+
    2023-08-16
    android
  • Android自定义控件之自定义属性(二)
    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性。本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解。有关原理知识请参...
    99+
    2022-06-06
    属性 自定义属性 Android
  • Android怎么自定义双向滑动控件
    这篇文章主要介绍“Android怎么自定义双向滑动控件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android怎么自定义双向滑动控件”文章能帮助大家解决问题。先看一下效果图1.SeekBarPr...
    99+
    2023-06-30
  • Android实现在xml文件中引用自定义View的方法分析
    本文实例讲述了Android实现在xml文件中引用自定义View的方法。分享给大家供大家参考,具体如下: 在xml中引用自定义view 方法一: <view class...
    99+
    2022-06-06
    xml文件 XML view 方法 自定义view Android
  • android 自定义控件 使用declare
    在Android中,可以使用`declare-styleable`来定义和使用自定义控件的属性。下面是一个简单的示例:1. 在res...
    99+
    2023-09-21
    Android
  • 怎么中Android中自定义一个悬浮窗控件
    今天就跟大家聊聊有关怎么中Android中自定义一个悬浮窗控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步设计类似Toast的类FloatWindowpackage ...
    99+
    2023-05-31
    android roi %d
  • 如何在Android应用中自定义一个控件
    本篇文章为大家展示了如何在Android应用中自定义一个控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。开发自定义控件的步骤:了解View的工作原理 2、 编写继承自View的子类3、 为自定义V...
    99+
    2023-05-31
    android roi
  • Android怎么实现自定义折线图控件
    这篇“Android怎么实现自定义折线图控件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android怎么实现自定义折线图...
    99+
    2023-07-02
  • Android自定义覆盖层控件 悬浮窗控件
    在我们移动应用开发过程中,偶尔有可能会接到这种需求: 1、在手机桌面创建一个窗口,类似于360的悬浮窗口,点击这个窗口可以响应(至于窗口拖动我们可以后面再扩展)。 2、自己...
    99+
    2022-06-06
    Android
  • 【Android自定义控件】自适应文字大小的TextView
    Android8.0以上可以在TextView中添加autoSizeTextType实现 Android8.0以下要想达到根据TextView大...
    99+
    2022-06-06
    自适应 Android
  • android 自定义控件 自定义属性详细介绍
    自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地...
    99+
    2022-06-06
    自定义 自定义控件 属性 自定义属性 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作