1. 程式人生 > >自定義顏色的水平進度條

自定義顏色的水平進度條

SeekBar和ProgressBar

佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:progressDrawable="@drawable/layer_list_seekbar" />

    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:progressDrawable="@drawable/layer_list_progressbar" />
</LinearLayout>

注意:

android:progressDrawable="@drawable/layer_list_progressbar"

該屬性: 設定進度中的背景顏色和進度顏色

layer_list_seekbar

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="3dp" />
            <gradient
            android:angle="270"
            android:centerColor="#2B2B2B"
            android:centerY="0.75"
            android:endColor="#ff747674"
            android:startColor="#ff9d9e9d" />
            <solid android:color="#2B2B2B" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="3dp" />
                <gradient
                    android:angle="270"
                    android:centerColor="#ff3399CC"
                    android:centerY="0.75"
                    android:endColor="#ff6699CC"
                    android:startColor="#ff0099CC" />
                <solid android:color="#2496F6" />
            </shape>
        </clip>
    </item>
</layer-list>

layer_list_progressbar

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="2dp" />
            <gradient
                android:angle="270"
                android:centerColor="@color/colorPrimaryDark"
                android:endColor="#ff4801"
                android:startColor="#f5f5f5" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dp" />
                <gradient
                    android:centerColor="#ff7612"
                    android:endColor="#ff7612"
                    android:startColor="#ff7612" />
            </shape>
        </clip>
    </item>

</layer-list>

注意:

  <item android:id="@android:id/background">
設定背景

 <item android:id="@android:id/progress">
設定進度

MainActivity

public class MainActivity extends AppCompatActivity {

    private ProgressBar mPb;
    private SeekBar mSb;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPb = ((ProgressBar) this.findViewById(R.id.pb));
        mSb = ((SeekBar) this.findViewById(R.id.sb));

        mSb.setProgress(50);
        mPb.setProgress(50, true);

    }


}

 可以滿足一般的進度條設定。