• Compose进度条


    Compose进度条

    圆形进度条

    1. CircularProgressIndicator(
    2. //0.0表示没有进度,1.0表示已完成进度
    3. progress = index.toFloat(),
    4. //大小设置
    5. modifier = Modifier.size(50.dp),
    6. //颜色设置
    7. color = Color.Red,
    8. //宽度设置
    9. strokeWidth = 4.dp,
    10. )

    圆形进度条用CircularProgressIndicator组件:

    progress属性设置进度条当前进度值,类型为Float,设置参数范围为0.0~1.0。

    color属性设置进度条颜色。

    strokeWidth属性设置进度条宽度。

    条形进度条

    1. LinearProgressIndicator(
    2. //0.0表示没有进度,1.0表示已完成进度
    3. progress = index.toFloat(),
    4. modifier = Modifier.padding(10.dp),
    5. color = Color.Yellow,
    6. )

    条形进度条用LinearProgressIndicator组件。

    完整代码:

    1. import android.os.Bundle
    2. import androidx.activity.ComponentActivity
    3. import androidx.activity.compose.setContent
    4. import androidx.compose.foundation.BorderStroke
    5. import androidx.compose.foundation.layout.Column
    6. import androidx.compose.foundation.layout.fillMaxSize
    7. import androidx.compose.foundation.layout.padding
    8. import androidx.compose.foundation.layout.size
    9. import androidx.compose.foundation.shape.RoundedCornerShape
    10. import androidx.compose.material3.Button
    11. import androidx.compose.material3.ButtonDefaults
    12. import androidx.compose.material3.CircularProgressIndicator
    13. import androidx.compose.material3.LinearProgressIndicator
    14. import androidx.compose.material3.MaterialTheme
    15. import androidx.compose.material3.Surface
    16. import androidx.compose.material3.Text
    17. import androidx.compose.runtime.Composable
    18. import androidx.compose.runtime.mutableStateOf
    19. import androidx.compose.runtime.saveable.rememberSaveable
    20. import androidx.compose.ui.Alignment
    21. import androidx.compose.ui.Modifier
    22. import androidx.compose.ui.graphics.Color
    23. import androidx.compose.ui.res.colorResource
    24. import androidx.compose.ui.res.stringResource
    25. import androidx.compose.ui.tooling.preview.Preview
    26. import androidx.compose.ui.unit.dp
    27. import com.cwj.composedemo.ui.theme.ComposeDemoTheme
    28. class MainActivity : ComponentActivity() {
    29. override fun onCreate(savedInstanceState: Bundle?) {
    30. super.onCreate(savedInstanceState)
    31. setContent {
    32. ComposeDemoTheme {
    33. // A surface container using the 'background' color from the theme
    34. Surface(
    35. modifier = Modifier.fillMaxSize(),
    36. color = MaterialTheme.colorScheme.background
    37. ) {
    38. val index = rememberSaveable { mutableStateOf(0.2) }
    39. Greeting(index.value) {
    40. index.value = it
    41. }
    42. }
    43. }
    44. }
    45. }
    46. }
    47. @Composable
    48. fun Greeting(index: Double, onIndexChangeInt: (Double) -> Unit) {
    49. Column(
    50. modifier = Modifier.fillMaxSize(),
    51. //横向居中
    52. horizontalAlignment = Alignment.CenterHorizontally
    53. ) {
    54. //圆形进度条
    55. CircularProgressIndicator(
    56. //0.0表示没有进度,1.0表示已完成进度
    57. progress = index.toFloat(),
    58. //大小设置
    59. modifier = Modifier.size(50.dp),
    60. //颜色设置
    61. color = Color.Red,
    62. //宽度设置
    63. strokeWidth = 4.dp,
    64. )
    65. //条形进度条
    66. LinearProgressIndicator(
    67. //0.0表示没有进度,1.0表示已完成进度
    68. progress = index.toFloat(),
    69. modifier = Modifier.padding(10.dp),
    70. color = Color.Yellow,
    71. )
    72. Button(
    73. //边框宽度与颜色设置
    74. border = BorderStroke(2.dp, color = colorResource(id = R.color.purple_200)),
    75. //圆角设置
    76. shape = RoundedCornerShape(8.dp),
    77. colors = ButtonDefaults.buttonColors(
    78. //容器颜色
    79. containerColor = colorResource(id = R.color.purple_500),
    80. //内容颜色
    81. contentColor = colorResource(id = R.color.white),
    82. ),
    83. onClick = {
    84. if (index >= 1) {
    85. onIndexChangeInt(0.0)
    86. } else {
    87. onIndexChangeInt(index + 0.2)
    88. }
    89. }
    90. ) {
    91. Text(
    92. text = stringResource(id = R.string.add),
    93. )
    94. }
    95. }
    96. }
    97. @Preview(showBackground = true, showSystemUi = true)
    98. @Composable
    99. fun GreetingPreview() {
    100. ComposeDemoTheme {
    101. val index = rememberSaveable { mutableStateOf(0.2) }
    102. Greeting(index.value) {
    103. index.value = it
    104. }
    105. }
    106. }

  • 相关阅读:
    prototype-based learning algorithm(原型学习)
    Scala语言基础(2)
    Java实现微信支付功能
    从电竞男孩到CEO,他如何用电子签实现事业腾飞
    Word文档超过了20MB如何缩小?文件压缩这样做
    C++ 类成员指针
    如何将达梦数据库连接到 ONLYOFFICE 文档编辑器
    代码随想录 Day - 62|#84 柱状图中最大的矩阵
    接口测试在python、jmeter、postman工具下如何做断言?
    项目范围管理
  • 原文地址:https://blog.csdn.net/juer2017/article/details/133746638