尝试更改 Android ProgressBar 的角半径时出错
Error when trying to change the corner radius of Android ProgressBar
我有一个 ProgressBar
小部件 Widget.AppCompat.ProgressBar.Horizontal
样式:
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在深入研究 AppCompat 源代码中的 Widget.AppCompat.ProgressBar.Horizontal
(它在内部使用 Widget.Material
)样式定义时,我发现对于 API 21+,它使用了以下可绘制对象:
<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal">
<item name="progressDrawable">@drawable/progress_horizontal_material</item>
...
</style>
这是progress_indeterminate_horizontal_material
的片段:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/background"
android:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle"
android:tint="?attr/colorProgressBackgroundNormal">
<corners android:radius="?attr/progressBarCornerRadius" />
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</item>
它使用 ?attr/colorProgressBackgroundNormal
属性,但是当我尝试使用以下行在我的应用程序主题中设置此属性的值时:
<item name="progressBarCornerRadius">2dp</item>
然后我遇到编译错误:
AAPT: error: style attribute 'attr/progressBarCornerRadius (aka com.application:attr/progressBarCornerRadius)' not found.
如何让进度条变成弧形的?
Replace the element, with . That will make the shape keep its
form as it grows - and not cut off. Unfortunately, it will have a
little unwanted visual effect at the beginning - as the shape corners
don't have enough space to draw. But it might be good enough for most
cases.
完整代码:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/dirtyWhite"/>
</shape>
</item>
<item android:id="@android:id/progress"
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
</scale>
</item>
</layer-list>
output
属性名称是 android:progressBarCornerRadius
但它是私有的。
您可以使用具有自定义样式的 ProgressBar
:
<style name="customProgressBar" parent="Widget.AppCompat.ProgressBar.Horizontal">
<item name="progressDrawable">@drawable/custom_progress_horizontal</item>
</style>
否则,您可以使用 Material 组件库提供的新 LinearProgressIndicator
:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:indeterminate="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:trackThickness="10dp"
app:trackCornerRadius="8dp"/>
注意: 至少需要版本 1.3.0-alpha04
.
我有一个 ProgressBar
小部件 Widget.AppCompat.ProgressBar.Horizontal
样式:
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在深入研究 AppCompat 源代码中的 Widget.AppCompat.ProgressBar.Horizontal
(它在内部使用 Widget.Material
)样式定义时,我发现对于 API 21+,它使用了以下可绘制对象:
<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal">
<item name="progressDrawable">@drawable/progress_horizontal_material</item>
...
</style>
这是progress_indeterminate_horizontal_material
的片段:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@id/background"
android:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle"
android:tint="?attr/colorProgressBackgroundNormal">
<corners android:radius="?attr/progressBarCornerRadius" />
<size android:height="@dimen/progress_bar_height_material" />
<solid android:color="@color/white_disabled_material" />
</shape>
</item>
它使用 ?attr/colorProgressBackgroundNormal
属性,但是当我尝试使用以下行在我的应用程序主题中设置此属性的值时:
<item name="progressBarCornerRadius">2dp</item>
然后我遇到编译错误:
AAPT: error: style attribute 'attr/progressBarCornerRadius (aka com.application:attr/progressBarCornerRadius)' not found.
如何让进度条变成弧形的?
Replace the element, with . That will make the shape keep its form as it grows - and not cut off. Unfortunately, it will have a little unwanted visual effect at the beginning - as the shape corners don't have enough space to draw. But it might be good enough for most cases.
完整代码:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/dirtyWhite"/>
</shape>
</item>
<item android:id="@android:id/progress"
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
</scale>
</item>
</layer-list>
output
属性名称是 android:progressBarCornerRadius
但它是私有的。
您可以使用具有自定义样式的 ProgressBar
:
<style name="customProgressBar" parent="Widget.AppCompat.ProgressBar.Horizontal">
<item name="progressDrawable">@drawable/custom_progress_horizontal</item>
</style>
否则,您可以使用 Material 组件库提供的新 LinearProgressIndicator
:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:indeterminate="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:trackThickness="10dp"
app:trackCornerRadius="8dp"/>
注意: 至少需要版本 1.3.0-alpha04
.