一、控件效果展示

二、控件类:OptionsItemView.java

/*** 选项Item* * @author : ZGS* Created on 2021/11/16**/
public class OptionItemView extends ConstraintLayout {private TextView tvLeft;private TextView tvRight;private ImageView ivRight;private View line;public OptionItemView(Context context, AttributeSet attrs) {super(context, attrs);init(context,attrs);}public OptionItemView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context,attrs);}public OptionItemView(@NonNull @NotNull Context context) {super(context);init(context,null);}private void init(Context context, AttributeSet attrs){LayoutInflater.from(context).inflate(R.layout.view_options_item,this);tvLeft = findViewById(R.id.tvLeft);tvRight = findViewById(R.id.tvRight);ivRight = findViewById(R.id.ivRight);line = findViewById(R.id.line);TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.OptionItemView);String tvLeftText = params.getString(R.styleable.OptionItemView_tvLeftText);String tvRightText = params.getString(R.styleable.OptionItemView_tvRightText);Integer ivRightRes = params.getResourceId(R.styleable.OptionItemView_ivRight,R.drawable.ic_right);setTvLeftText(tvLeftText);setTvRightText(tvRightText);setIvRightResource(ivRightRes);params.recycle();}public void setOperationListener(OnClickListener listener) {ivRight.setOnClickListener(listener);}public void setTvLeftText(String text){tvLeft.setText(text);}public void setTvRightText(String text){tvRight.setText(text);}public void setIvRightResource(Integer rId){ivRight.setImageResource(rId);}
}

三、控件布局:view_options_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><!--左边文本--><TextViewandroid:id="@+id/tvLeft"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginEnd="16dp"android:gravity="left|center_vertical"android:text="TextView"android:textColor="@color/black"android:textSize="18sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toStartOf="@+id/tvRight"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><!--右边文本--><TextViewandroid:id="@+id/tvRight"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"android:textSize="18sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintEnd_toStartOf="@+id/ivRight"app:layout_constraintTop_toTopOf="parent" /><!--右边按钮--><ImageViewandroid:id="@+id/ivRight"android:layout_width="40dp"android:layout_height="40dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="parent"app:srcCompat="@drawable/ic_right" /><!--分割线--><Viewandroid:id="@+id/line"android:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginLeft="16dp"android:background="@color/teal_200"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

四、使用控件:activity_user.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"android:orientation="vertical"tools:context=".ui.user.UserActivity"><com.sziit.bluetoothassistant.view.OptionItemViewandroid:id="@+id/option1"android:layout_width="match_parent"android:layout_height="100dp"app:tvLeftText="个人信息"app:tvRightText="编辑" /><com.sziit.bluetoothassistant.view.OptionItemViewandroid:id="@+id/option2"android:layout_width="match_parent"android:layout_height="100dp"app:tvLeftText="修改密码"app:tvRightText="编辑" />
</androidx.appcompat.widget.LinearLayoutCompat>

五、控件点击处理:UserActivity.java

public class UserActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_user);OptionItemView option1 = findViewById(R.id.option1);option1.setOperationListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO 在这里添加点击处理Toast.makeText(UserActivity.this, "点击处理", Toast.LENGTH_SHORT).show();}});}
}

Android自定义控件系列(1):选项控件相关推荐

  1. Android自定义控件ImageViwe(一)——依据控件的大小来设置缩放图片显示

    功能: 自定义 ImageView 设置显示图片,如果图片的宽与高小于控件的宽与高,就将图片设置显示到控件的中央, 如果图片的宽与高有一项大于控件的宽与高,那么就将图片进行缩放显示,两者者是显示在控件 ...

  2. android 自定义控件ondraw,android--------自定义控件 之 方法篇

    前面简单的讲述了Android中自定义控件的理论和流程图,今天通过代码来详细的讲解一下其中的方法 首先先创建一个类 CircularView 继承于 View,之后实现构造方法(初始化步骤) publ ...

  3. Android 自定义控件 ViewPager头部指示器控件 ViewPagerBelowIndicator

    效果 演示 说明 为了实现 ViewPager 切换 Fragment 时的标签效果(类似新闻客户端导航的效果) 代码 package com.demo.view;import android.con ...

  4. android 原理 组合控件_Android自定义控件进阶01-自定义控件开发套路与流程

    Android自定义控件进阶01-自定义控件开发套路与流程本章节为什么要叫进阶篇?(虽然讲的是基础内容),因为从本篇开始,将会逐渐揭开自定义View的神秘面纱,每一篇都将比上一篇内容更加深入,利用所学 ...

  5. android shape 无边框颜色,Android 使用shape定义不同控件的的颜色、背景色、边框色...

    Android 使用shape定义不同控件的的颜色.背景色.边框色 设置按钮的右边框和底边框颜色为红色,边框大小为3dp: 在drawable新建一个 buttonstyle.xml的文件,内容如下: ...

  6. Android自定义控件系列之基础篇

    一.概述 在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大 ...

  7. android excel布局,Androidui布局控件(2)表格布局excelPanel

    推荐理由 RecycleView定制组件 支持向左,向右加载过去,未来数据 列行表头锁定 作为表格布局,可以作为您的ui公共组件库里面的一员,你可以二次开发,让它变得更加可定制化,可扩展,更强大 In ...

  8. Android自定义控件系列八:详解onMeasure()(二)--利用onMeasure测量来实现图片拉伸永不变形,解决屏幕适配问题

    上一篇文章详细讲解了一下onMeasure/measure方法在Android自定义控件时的原理和作用,参看博文:Android自定义控件系列七:详解onMeasure()方法中如何测量一个控件尺寸( ...

  9. Study on Android【四】--显示控件使用

    Android的界面显示同样也是基于控件的.通常是用View(包括ViewGroup)控件配上XML的样式来做的.具体细节不想说了,可以参考 Samples里的ApiDemos/View,和View的 ...

  10. android 电量控件,Android实现显示电量的控件代码

    下面介绍了Android实现显示电量的控件代码,具体代码如下: 1.目录结构,本人是使用安卓死丢丢. 2.运行界面,输入框中输入数值,点击刷新,会再电池中显示出相应的电量 3.绘制自定义电池控件,首先 ...

最新文章

  1. JAVA核心技术I---JAVA基础知识(查漏补缺private,static)
  2. 卷积神经网络「失陷」,CoordConv来填坑
  3. Android之getSystemService
  4. mysql关联查询去重_MySQL外键和高级查询(连接查询、联合查询、子查询、去重查询)...
  5. 详解京东商城智能对话系统(生成+检索)
  6. linux 多域名绑定
  7. lodash源码分析之compact中的遍历
  8. html5录音功能代码,recorder.js 基于 HTML5 实现录音功能
  9. java中json数据_java中的JSON对象的使用
  10. Spring: ConfigurationClassUtils类
  11. SpringBoot POM说明
  12. UML--交互图(时序图、协作图)
  13. c#app.config配置文件使用
  14. PHP执行linux系统命令
  15. libsvm 的使用
  16. NYOJ599 - 奋斗的小蜗牛
  17. Gartner预测公有云将迎来“双头垄断”局面
  18. 简简单单利用字典破解zip文件口令
  19. ubuntu 查找opencv安装路径_ubuntu 查找opencv安装路径_Ubuntu安装opencv详细步骤
  20. Java程序设计课程设计_《JAVA程序设计》课程设计

热门文章

  1. OpenCV系列之Canny边缘检测 | 十九
  2. 2022-4-30 Leetcode199.二叉树的右视图
  3. 干货 | 10分钟给上万客服排好班,携程大规模客服排班算法实践
  4. 学习Linux命令(23)
  5. linux下查看MBR实例分析
  6. Python 于 webgame 的应用(下)
  7. zz让创意大赛点燃大学生创新激情
  8. 2021-07-02Leetcode236.二叉树的最近公共祖先
  9. Metasploit数据库连接及其简单应用
  10. 5g的八大关键指标_【技术·航天】5G与卫星通信融合应用前景浅析