进度条的使用用户等待时较好的体验,常用的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属性,设置进度中的背景颜色和进度颜色。
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>
注意:@android:id/background设置背景颜色,@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);
}
}
可以满足一般的进度条颜色、渐变色设置。