自定义颜色的水平进度条

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36347817/article/details/81315763

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);

    }


}

 可以满足一般的进度条设置。

猜你喜欢

转载自blog.csdn.net/qq_36347817/article/details/81315763