效果图:
XML布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/sb_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sb01"
android:background="@drawable/price"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="12sp" />
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv01"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:max="100"
android:maxHeight="20px"
android:minHeight="20px"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:progress="0"
android:progressDrawable="@drawable/seekbar_define_style"
android:thumb="@drawable/sliding_button" />
</LinearLayout>
JAVA代码:
SeekBar sb=(SeekBar) popupWindow.findViewById(R.id.sb);
final TextView sb_text=(TextView) popupWindow.findViewById(R.id.sb_text);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar seekbar1, int arg1, boolean arg2) {
int position=seekbar1.getProgress();
//seekbar的宽度,一定记得要减去内边距!否则设置textView的位移时会不准
float seekbarWidth=seekbar1.getWidth()-DensityUtil.dp2px(B1_CarDetailActivity.this, 40);
float translateX=(position * seekbarWidth)/100;
sb_text.setText(position+"");
sb_text.setTranslationX(translateX);
}
});
自定义进度条样式:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/sliding_region_down"/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/sliding_region"/>
<!--<item-->
<!--android:id="@android:id/secondaryProgress"-->
<!--android:drawable="#FF7E00"/>-->
</layer-list>
时间: 2024-10-12 10:40:11