10

有没有办法自定义 MediaController?我需要更改按钮、SeekBar 等的样式。

4

3 回答 3

8

您可以做的是递归 MediaController 的视图层次结构并以编程方式设置 SeekBar 的可绘制对象:

private void styleMediaController(View view) {
    if (view instanceof MediaController) {
        MediaController v = (MediaController) view;
        for(int i = 0; i < v.getChildCount(); i++) {
            styleMediaController(v.getChildAt(i));
        }
    } else
        if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                styleMediaController(ll.getChildAt(i));
            }
        } else if (view instanceof SeekBar) {
            ((SeekBar) view).setProgressDrawable(getResources().getDrawable(R.drawable.progressbar)); 
            ((SeekBar) view).setThumb(getResources().getDrawable(R.drawable.progresshandle));
        }
}

然后,只需调用

styleMediaController(myMC);
于 2012-10-03T23:28:22.957 回答
3

该方法makeControllerView旨在被覆盖,以便您可以提供自己的视图。不幸的是,它目前是隐藏的。

您可能想要获取 MediaController 的源代码并重新实现它或将隐藏的方法复制并粘贴到子类中,以便您可以自定义它。

于 2010-01-11T20:00:48.723 回答
3

我更改了 bk138 答案的代码以更改元素的颜色。不是可绘制对象本身。此解决方案与支持库 v4 一起兼容旧设备。

private void styleMediaController(View view) {
        if (view instanceof MediaController) {
            MediaController v = (MediaController) view;
            for (int i = 0; i < v.getChildCount(); i++) {
                styleMediaController(v.getChildAt(i));
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for (int i = 0; i < ll.getChildCount(); i++) {
                styleMediaController(ll.getChildAt(i));
            }
        } else if (view instanceof SeekBar) {
            ((SeekBar) view)
                    .getProgressDrawable()
                    .mutate()
                    .setColorFilter(
                            getResources().getColor(
                                    R.color.MediaPlayerMeterColor),
                            PorterDuff.Mode.SRC_IN);
            Drawable thumb = ((SeekBar) view).getThumb().mutate();
            if (thumb instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
                //compat mode, requires support library v4
                ((android.support.v4.graphics.drawable.DrawableWrapper) thumb).setCompatTint(getResources()
                        .getColor(R.color.MediaPlayerThumbColor));
            } else {
                //lollipop devices
                thumb.setColorFilter(
                        getResources().getColor(R.color.MediaPlayerThumbColor),
                        PorterDuff.Mode.SRC_IN);
            }
        }
    }

然后,只需调用

styleMediaController(myMC);

不得不打电话styleMediaController(myMC)给它OnPreparedListenerVideoView使其工作。否则 MediaController 视图没有子视图。

于 2016-03-23T08:37:30.290 回答