自定义属性attr
我们在Android中的res--values下创建了一个attr.xml,名称无关紧要.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressButton">
<attr name="backgroundcolor" format="color" />
<attr name="foreground" />
<attr name="textcolor" format="color" />
<attr name="max" />
<attr name="progress" />
<attr name="textSize" />
<attr name="text" format="string" />
</declare-styleable>
</resources>
通过以上两个步骤,实现了我们的ProgressButton自定义控件. 接下来,我们将了解如何使用它.

使用ProgressButton
微信智慧
activity_main.xml
在主窗体的布局文件中,您可以直接添加我们刚刚生成的ProgressButton.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<dem.vac.progressbutton.ProgressButton
android:layout_gravity="center"
android:layout_width="100dp"
android:layout_height="50dp"
app:backgroundcolor="@color/colorLightSkyBlue"
app:foreground="@color/colorRoyalBlue"
app:textcolor="@color/colorWhite"
app:text="点击开始"
android:id="@+id/progressbtn" />
</LinearLayout>
MainActivity.kt

接下来看一下我们主程序的代码
package dem.vac.progressbutton import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlin.concurrent.thread class MainActivity : AppCompatActivity() { lateinit var probtn: ProgressButton override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) probtn = findViewById(R.id.progressbtn) probtn.setOnProgressButtonClickListener(object : ProgressButton.OnProgressButtonClickListener { override fun onClickListener() { thread { for (i in 1..100) { probtn.setProgress(i) probtn.setText("当前$i") Thread.sleep(50) } } } }) } }
在代码中android进度条的使用,我们直接调用ProgressButton,然后重写onProgressButtonClickListerner事件. 在这种情况下,我们启动一个线程并执行1到100的循环. 该调用的效果是本文开头的效果.
此分享来自公开微信的号码-智享卡(VaccaeShare),作者: Vaccae
有关原始来源和转载信息的详细信息android进度条的使用,请访问yunjia_community@tencent.com删除任何侵权行为.
最初发表于: 2019-12-09
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-161213-2.html
你一来正好給我们一个接口把人造岛军事化