android - 进度条没有填满它应该填满的空间

标签 android android-layout kotlin progress-bar android-progressbar

我正在努力实现这样的目标:

enter image description here

我尝试使用卡片 View 并以此结束:

enter image description here

我遇到了一个问题,因为我希望“剩余水”进度条转到卡片的末尾(如图1所示),但它不会改变它的长度(而且我不希望硬编码那么长),有什么想法我能做什么吗?

(代码:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:backgroundTint="@color/white"
    android:orientation="horizontal"
    card_view:cardCornerRadius="40dp"
    card_view:cardElevation="5sp"
    card_view:contentPadding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tools:srcCompat="@tools:sample/avatars" />
        </LinearLayout>

        <TableLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:text="name:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/plc_ModelId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:textSize="14dp"
                    tools:text="12345678" />
            </TableRow>

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="3dp"
                    android:text="water left:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <ProgressBar
                    android:id="@+id/progressBar2"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp" />

            </TableRow>

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:text="time left:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/plc_Version"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:textSize="14sp"
                    tools:text="00h 20 m" />
            </TableRow>

        </TableLayout>

    </LinearLayout>
</androidx.cardview.widget.CardView>

最佳答案

要实现全宽,请在 ProgressBar 中使用 android:layout_weight="1"

        <TableRow>

            <TextView
                android:text="water left:"
             ../>

            <ProgressBar
                android:id="@+id/progressBar2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
               />

        </TableRow>

enter image description here

还可以使用 Material 组件库提供的 LinearProgressIndicatortrackThickness 属性来实现不同的高度:

类似于:

  <com.google.android.material.progressindicator.LinearProgressIndicator
            android:id="@+id/progressBar2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            card_view:trackThickness="12dp"
            card_view:trackColor="@color/white"
            card_view:indicatorColor="@color/red600Light"
            android:progress="75"
            android:layout_margin="3dp"
            />

enter image description here

关于android - 进度条没有填满它应该填满的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66908244/

相关文章:

java - 从外部 fragment 内的 FragmentTabHost 的子 fragment 添加新的选项菜单

java - setText 在 while 循环中不起作用

android - 给定资源值的 <color> 无效

android - 如何正确使用INSERT OR REPLACE?

java - 如何修复 LocationManager 在初始化时崩溃的应用程序

android ScrollView 最小化我的项目高度

java - 当在 CustomView 中扩展 ArrayAdapter 时,将 Kotlin 翻译为 Android 中的 Java

android - 如何在 EditText 中设置字符限制?

机器人 : ACTION_PACKAGE_ADDED not received (xperia SO-03D)

java - 使用 IntelliJ 将 Java lambda 转换为 Kotlin Lambda 生成语法错误