达标进度条

1.效果

1.

在这里插入图片描述

2.代码与使用

1.自定义组合控件

kotlin

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.example.customview3.R/*** 创建日期:2023/11/19 0:01* @author 唐门.西门吹雪* 类说明:*/
class UpToParView(private val context: Context, attrs:AttributeSet):LinearLayout(context,attrs) {private lateinit var mDrawableEnd: Drawableprivate lateinit var mNoDrawable: Drawableprivate lateinit var mYesDrawable: Drawableprivate var mLineYesColor: Int = 0private var mLineNoColor: Int = 0private var mAll: Int=3private var mAttain: Int=0private var mWidth: Float=0fprivate var mLineHeight: Float=0fprivate var mLastMargin: Float=0fprivate var mLlView:LinearLayoutprivate var mLlIv:LinearLayoutinit {initListener()LayoutInflater.from(context).inflate(R.layout.view_up_par,this,true)mLlView=findViewById(R.id.ll_view)mLlIv=findViewById(R.id.ll_iv)}private fun initListener() {}/*** 设置进度条达标的属性** @param all 进度条总达标数* @param attain 已达标数* @param width 达标正方形宽度* @param lineHeight 线的高度* @param lastMarginLeft 最好一个的左边距*/fun setParams(all: Int,attain: Int,width: Float,lineHeight: Float,lastMarginLeft: Float,lineNoColor: Int,lineYesColor: Int,noDrawable: Drawable,yesDrawable: Drawable,drawableEnd: Drawable) {this.mAll=allthis.mAttain=attainif (mAll<=mAttain||mAll<1||mAttain<0){visibility= View.GONEreturn}this.mWidth=widththis.mLineHeight=lineHeightthis.mLastMargin=lastMarginLeftthis.mLineNoColor=lineNoColorthis.mLineYesColor=lineYesColorthis.mYesDrawable=yesDrawablethis.mNoDrawable=noDrawablethis.mDrawableEnd=drawableEnd//根据总长度设置达标控件数setViewAll()}private fun setViewAll() {setViewLine()setIv()}/*** 设置线*/private fun setViewLine() {for (i in 0  until mAll){if (i>2&&i!=mAll-1){continue}val tv=TextView(context)tv.setBackgroundColor(if (i<mAttain){mLineYesColor}else{mLineNoColor})//是否达标来设置背景色//如果是第一个或者最后一个,设置长度为一半val lp=LayoutParams(if (i==0||i==mAll-1){mWidth/2}else{mWidth}.toInt(),mLineHeight.toInt())lp.setMargins((if (i==0){mWidth/2}else{0}).toInt(),0,0,0)lp.gravity= Gravity.CENTER_VERTICALtv.layoutParams=lpmLlView.addView(tv)}}/*** 设置达标的圆圈*/@SuppressLint("UseCompatLoadingForDrawables")private fun setIv() {for (i in 0  until mAll){if (i>2&&i!=mAll-1){continue}val iv=ImageView(context)iv.background=if (i==mAll-1){mDrawableEnd}else(if (i<mAttain){mYesDrawable}else{mNoDrawable})//如果是第一个或者最后一个,设置长度为一半val lp=LayoutParams(mWidth.toInt(),mWidth.toInt())lp.setMargins((if (i==mAll-1){mLastMargin}else{0}).toInt(),0,0,0)lp.gravity= Gravity.CENTER_VERTICALiv.layoutParams=lpmLlIv.addView(iv)}}}

xml

<?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="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/ll_view"android:layout_width="0dp"android:layout_height="50dp"android:orientation="horizontal"app:layout_constraintEnd_toEndOf="@id/ll_iv"app:layout_constraintStart_toStartOf="@id/ll_iv"app:layout_constraintTop_toTopOf="@id/ll_iv"app:layout_constraintBottom_toBottomOf="@id/ll_iv"android:gravity="center_vertical"></LinearLayout><LinearLayoutandroid:id="@+id/ll_iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

2.Activity

kotlin

class MainActivity : AppCompatActivity() {private lateinit var binding:ActivityMainBindingoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding= ActivityMainBinding.inflate(layoutInflater)setContentView(binding.root)val all=4val attain=2val width=resources.getDimension(R.dimen.my_50dp)val lineHeight=resources.getDimension(R.dimen.my_5dp)val lastMarginLeft=resources.getDimension(R.dimen.my_5dp)binding.utp.setParams(all,attain,width,lineHeight,lastMarginLeft,resources.getColor(R.color.gray),resources.getColor(R.color.black),resources.getDrawable(R.drawable.ic_launcher_foreground),resources.getDrawable(R.drawable.ic_launcher_foreground_black),resources.getDrawable(R.drawable.ic_launcher_foreground_end))}
}

xml

<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="@dimen/my_300dp"tools:context=".MainActivity"><com.example.customview3.viewgroup.UpToParViewandroid:id="@+id/utp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginTop="50dp"android:text="Hello World!"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

3.

4.

5.

6.

3.总结

1.

4.遇到的问题

1.

2.

3.

4.

5.

6.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/196097.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

进程之理解进程的概念

你必须非常努力&#xff0c;才能看起来毫不费力。文章目录 进程的基本概念描述进程——pcbtest_struct pcb的一种task_struct 内容分类 组织进程查看进程通过系统调用获取进程标示符总结 进程的基本概念 课本概念&#xff1a;进程是一个执行实列&#xff0c;正在执行的程序等。…

C语言——1.入门须知

文章目录 1.C语言的简要概述1.1.C语言类比自然语言1.2.计算机语言的发展1.3.C语言在当今的地位1.4.C语言的优势和劣势1.4.1.C语言的优势1.4.2.C语言的劣势 2.C语言的应用场景3.C语言的学习路径3.1.学习目的3.2.学习路径3.3.学习资源3.3.1.推荐书籍3.3.2.推荐课程3.3.3.推荐题库…

Codeforces Round 910 (Div. 2)(D~F)

1898D - Absolute Beauty 题意&#xff1a;给定长度为n的数组a和b&#xff0c;定义b数组的价值为&#xff0c;现可以交换一次b数组中的任意两个元素&#xff0c;求b数组的价值最大值。 思路&#xff1a;绝对值问题可以放在数轴上去解决。绝对值即为区间长度 观察上述三种情况&…

《微信小程序开发从入门到实战》学习十九

3.3 开发创建投票页面 3.3.7 wx:for列表渲染 接下来为创建的投票页面添加一个“添加选项”的功能。需要用户输入文字&#xff0c;应该使用input组件。头投票的数量是不确定的&#xff0c;面对不确定数量的组件的情况时&#xff0c;可以使用wx:for属性对组件进行列表渲染。 使…

【腾讯云 HAI域探秘】——即时职场生存指南小游戏以及【自行搭建Stable Diffusion图片AI绘制 | ChatGLM2-6B AI进行智能对话 | Pytorch2.0 AI框架视频处理】

利用HAI的ChatGLM2 6B做一个即时对话小游戏 ChatGLM2-6B 是开源中英双语对话模型 ChatGLM-6B 的第二代版本&#xff0c;在保留了初代模型对话流畅、部署门槛较低等众多优秀特性的基础之上&#xff0c;ChatGLM2-6B 引入了更强大的性能、更长的上下文、更高效的推理&#xff0c;…

git使用及常用命令

在初入公司中&#xff0c;若使用的是git管理工具&#xff0c;需要做以下步骤&#xff1a; 1&#xff0c;常用命令在&#xff1a; &#xff08;1&#xff09;&#xff0c;git config --global user.name xxx(名字) //若不设置 那么下次提交代码时会报错 其次该设置名字和…

【运维篇】Redis常见运维命令详解

文章目录 1. 前言2. 连接管理命令详解2.1 AUTH命令2.2 PING命令2.3 SELECT命令2.4 QUIT命令 3. 服务器管理命令详解3.1 FLUSHALL命令3.2 SAVE/BGSAVE命令3.3 SHUTDOWN命令 4. 安全管理命令详解4.1 CONFIG命令4.1.1 CONFIG SET命令用法4.1.2 CONFIG GET命令用法 4.2 AUTH命令 5.…

六、文件上传漏洞

下面内容部分&#xff1a;参考 一、文件上传漏洞解释 解释&#xff1a;文件上传漏洞一般指的就是用户能够绕过服务器的规则设置将自己的木马程序放置于服务器实现远程shell&#xff08;例如使用蚁剑远程连接&#xff09;&#xff0c;常见的木马有一句话木马(php) 无需启用sho…

【C++】【Opencv】霍夫直线检测即cv::HoughLinesP()函数详解和示例

cv::HoughLinesP()&#xff08;函数霍夫直线&#xff09;功能分析是一种用于检测图像中直线的算法&#xff0c;它基于霍夫变换的原理。通过该算法&#xff0c;我们可以从图像中提取出直线信息&#xff0c;从而对图像进行分析和处理。主要经理边缘检测和霍夫直线处理两个步骤。本…

EDA实验-----4*4矩阵键盘与数码管显示测试(Quartus ‖)

目录 一、实验目的 二、实验仪器设备 三、实验原理 四、实验要求 五、实验步骤 六、实验报告 七、实验过程 1.矩阵键盘按键原理 2.数码管原理 3.分频器代码 4.电路图连接 5.文件烧录 一、实验目的 了解数码管的工作原理&#xff1b;掌握4*4矩阵键盘和数码管显示的编…

[python]python筛选excel表格信息并保存到另一个excel

目录 关键词平台说明背景所需库1.安装相关库2.代码实现sourcetarget1 关键词 python、excel、DBC、openpyxl 平台说明 项目Valuepython版本3.6 背景 从一个excel表中遍历删选信息并保存到另一个excel表 所需库 1.openpyxl &#xff1a;是一个用于读写 Excel 文件的 Pyt…

C/C++统计数 2021年12月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析

目录 C/C统计数 一、题目要求 1、编程实现 2、输入输出 二、算法分析 三、程序编写 四、程序说明 五、运行结果 六、考点分析 C/C统计数 2021年12月 C/C编程等级考试一级编程题 一、题目要求 1、编程实现 给定一个数的序列S&#xff0c;以及一个区间[L, R], 求序列…