Android Seekbar 作为对话框

Android Seekbar as Dialog

我正在创建按钮来改变音量,我想要 onClickseekbar 显示为一种 dialog 但不是那个覆盖屏幕并且不允许与按钮下方屏幕上的其他元素交互) 并且它应该在几次 seconds.I 后消失不想使用 alertDialog android 规定涵盖了整个 activity.How 我要这样做吗?也有任何图书馆可以实现这一目标吗?

您可以使用 WindowManager 将您的 SeekBar 添加到 window,同时您可以与您的 activity 或其他人互动。 Post 一个处理程序在指定时间后删除该视图。

        View yourSeekBarView = inflator.inflate(R.layout.your_layout_xml,null,false);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                150,
                50,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);  
        params.gravity = Gravity.CENTER_HORIZONTAL |Gravity.TOP;      
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(oView, params);

wm.removeView(yourSeekBarView);

将 seekBar 放在 xml 布局中 alpha="0" 的按钮下方,然后尝试:

    SeekBar mSeekbar;
    int delay = 1000;
    Button mButton;
    Handler mHandler;
    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            Animation anim = new AlphaAnimation(1, 0);
            anim.setDuration(500);
            mSeekbar.startAnimation(anim);
        }
    };


    //inside onCreate (or wherever you decide)
    mHandler = new Handler(); //(android.os)
    mButton = (Button) findViewById(R.id.button1);

    mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            mHandler.removeCallbacks(mRunnable);
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mHandler.postDelayed(mRunnable, delay);
        }
    });


    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mSeekbar.getAlpha() == 0) {
                Animation anim = new AlphaAnimation(0, 1);
                anim.setDuration(500);
                anim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mHandler.postDelayed(mRunnable, delay);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mSeekbar.startAnimation(anim);
            }
        }
    });