当前位置:   article > 正文

Android SeekBar设置指示器标签,使用PopupWindow的方式

Android SeekBar设置指示器标签,使用PopupWindow的方式

给Android 原生的SeekBar控件添加一个指示器标签;记录一下

按下时弹出popupwindow,进度条更新时刷新pw,松开时关闭pw;

  1. public class SeekBarPopUtils {
  2. private static PopupWindow popWin = null;
  3. private static ConstraintLayout clPopPar = null;
  4. private static TextView tvPopTxt = null;
  5. public static void showPop(int progress,View seekBar){
  6. popWin = new PopupWindow();
  7. View mPopView = LayoutInflater.from(BaseApp.Companion.getAppContext()).inflate(R.layout.item_popup_win,null,false);
  8. clPopPar = mPopView.findViewById(R.id.cl_pop_par);
  9. tvPopTxt = mPopView.findViewById(R.id.tv_pop_txt);
  10. popWin.setContentView(mPopView);
  11. popWin.setHeight(dp2px(30));
  12. popWin.setWidth(seekBar.getWidth());
  13. popWin.showAsDropDown(seekBar,0,-(dp2px(30) + popWin.getHeight()));
  14. move(progress,seekBar);
  15. }
  16. public static void move(int progress,View seekBar){
  17. if (clPopPar != null){
  18. int tvPopWidth = dp2px(40);
  19. ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
  20. tvPopWidth, dp2px(30)
  21. );
  22. params.startToStart = clPopPar.getId();
  23. params.setMarginStart((seekBar.getWidth() - tvPopWidth)/100 * progress + tvPopWidth/3);
  24. tvPopTxt.setLayoutParams(params);
  25. tvPopTxt.setText(progress"");
  26. }
  27. }
  28. private static int dp2px(int dpVal){
  29. return (int) TypedValue.applyDimension(
  30. TypedValue.COMPLEX_UNIT_DIP,
  31. (float) dpVal,
  32. BaseApp.Companion.getAppContext().getResources().getDisplayMetrics()
  33. );
  34. }
  35. public static void dismiss(){
  36. popWin.dismiss();
  37. popWin = null;
  38. clPopPar = null;
  39. tvPopTxt = null;
  40. }
  41. }

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. android:id="@+id/cl_pop_par"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="30dp"
  6. android:layout_height="40dp"
  7. xmlns:app="http://schemas.android.com/apk/res-auto"
  8. xmlns:tools="http://schemas.android.com/tools">
  9. <TextView
  10. android:id="@+id/tv_pop_txt"
  11. android:layout_width="30dp"
  12. android:layout_height="40dp"
  13. android:gravity="center"
  14. android:paddingBottom="10px"
  15. app:layout_constraintStart_toStartOf="parent"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. android:textSize="14sp"
  18. android:background="@mipmap/popup_center_seekbar_qipao"
  19. android:textColor="#fff"
  20. tools:text = "55" />
  21. </androidx.constraintlayout.widget.ConstraintLayout>

使用:

  1. mSeekbar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
  2. override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
  3. SeekBarPopUtils.move(progress,seekBar!!)
  4. }
  5. override fun onStartTrackingTouch(seekBar: SeekBar?) {
  6. SeekBarPopUtils.showPop(seekBar!!)
  7. }
  8. override fun onStopTrackingTouch(seekBar: SeekBar?) {
  9. SeekBarPopUtils.dismiss()
  10. }
  11. })

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号