如何将水平不确定进度条(Horizontal Indeterminate ProgressBar)设为固定宽度
Custom Horizontal Indeterminate ProgressBar in Android
Looks like you’ve got a solid start on a custom loading progress bar! Let’s break down your existing setup and fill in the gaps for the custom drawable you referenced.
First, here’s your original ProgressBar code (formatted for clarity):
<ProgressBar android:id="@+id/loading_progressbar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminate="true" android:indeterminateBehavior="repeat" android:indeterminateDuration="1000" android:indeterminateDrawable="@drawable/custom_loading_progressbar" />
Key Attribute Breakdown
Let’s go through what each of these does to make sure you’re getting the behavior you want:
style="@style/Widget.AppCompat.ProgressBar.Horizontal": Uses AppCompat’s horizontal progress bar style, ensuring consistent rendering across older Android versions (down to API 14).android:indeterminate="true": Enables indeterminate mode—perfect for tasks where you don’t know exact progress (like server data loading).android:indeterminateBehavior="repeat": Makes the animation loop continuously instead of bouncing back and forth (the alternative iscycle, which reverses direction each cycle).android:indeterminateDuration="1000": Sets the length of one full animation cycle (1 second here). Adjust this to speed up or slow down the loading effect.android:indeterminateDrawable="@drawable/custom_loading_progressbar": This is where your custom visual style lives—let’s create that drawable next.
Creating the Custom Indeterminate Drawable
Create a new file res/drawable/custom_loading_progressbar.xml with the following code. This will give you a rounded, sliding progress bar that matches your animation duration:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Background track (gray bar behind the progress) --> <item android:id="@android:id/background"> <shape android:shape="rectangle"> <solid android:color="#E0E0E0" /> <!-- Light gray background --> <corners android:radius="4dp" /> <!-- Rounded corners --> </shape> </item> <!-- Animated progress clip --> <item android:id="@android:id/progress"> <clip> <shape android:shape="rectangle"> <solid android:color="#2196F3" /> <!-- Primary blue progress color --> <corners android:radius="4dp" /> </shape> <!-- Slide animation from left to right --> <translate android:fromXDelta="-100%" android:toXDelta="100%" android:duration="1000" <!-- Matches your ProgressBar's duration --> android:repeatCount="infinite" android:interpolator="@android:interpolator/linear" <!-- Smooth, constant speed --> /> </clip> </item> </layer-list>
Customization Tips
- Theme Matching: Replace the
solidcolors with theme attributes (like?attr/colorPrimary) to match your app’s design system. - Corner Radius: Adjust the
cornersradius to make the bar fully rounded (match the bar’s height) or square. - Animation Speed: Sync the
durationvalue in both the ProgressBar and drawable to keep the animation consistent. Lower values speed it up; higher values slow it down. - Visibility Control: Toggle the bar’s visibility in your activity/fragment based on loading state:
// Kotlin example val loadingBar = findViewById<ProgressBar>(R.id.loading_progressbar) loadingBar.visibility = View.VISIBLE // Show when loading starts loadingBar.visibility = View.GONE // Hide when loading completes
内容的提问来源于stack exchange,提问作者edwoollard
相关产品推荐
相关产品推荐

