You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将水平不确定进度条(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 is cycle, 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 solid colors with theme attributes (like ?attr/colorPrimary) to match your app’s design system.
  • Corner Radius: Adjust the corners radius to make the bar fully rounded (match the bar’s height) or square.
  • Animation Speed: Sync the duration value 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:00:56