在WearOS上实现全屏进度条

3

我正在尝试在WearOS手表上获得一个环绕整个屏幕边缘的ProgressBar,就像这样:

enter image description here

我认为创建这样的布局会起作用,但实际上它只是在屏幕中央创建了一个大ProgressBar

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
            
    <ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.Material.ProgressBar.Large"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

更新

我发现你可以使用 layer-list 来改变 ProgressBar 的大小:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
 
          <shape
                android:radius="18dp"
                android:shape="ring"
                android:thickness="2dp"
                android:useLevel="true">
            </shape>
    </item>
</layer-list>

如果您可以以编程方式将android:radius设置为屏幕大小,那么它可能会起作用。可以尝试类似以下代码:

GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setInnerRadius(18);
((ProgressBar)findViewById(R.id.progress_bar)).setProgressDrawable(gradientDrawable);

你可以使用顶层的FrameLayout吗?并且让ProgressBar匹配父容器吗? - Yuri Schimke
我花了更多时间,你可以通过将layer-list可绘制对象分配给ProgressBar并向可绘制对象添加一个shape节点,然后将android:radius属性设置为屏幕大小来使ProgressBar到达屏幕边缘,但是这太麻烦了,需要使用GradientDrawable。我会更新我的问题并分享我的进展。 - Kris B
1个回答

1

是的,但它只适用于Compose,而我正在使用XML视图。遗憾的是,在我构建应用程序时还没有Compose。 - Kris B
你可以扩展AbstractComposeView来在两者之间建立桥梁。https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView - Yuri Schimke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接