【问题标题】:Change style of android MediaController改变android MediaController的风格
【发布时间】:2010-01-11 18:37:18
【问题描述】:

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

【问题讨论】:

标签: android android-widget


【解决方案1】:

您可以做的是递归 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);

【讨论】:

  • 您的代码对我不起作用。参数index是什么原因? v.getChildCount() 每次都返回 0。
  • @rekire 需要 index 参数,因为该函数被自身递归调用,递减 MediaController 视图的视图层次结构。
  • 但变量index从未使用过。
  • 这有一些意义;-) 编辑帖子以删除多余的论点,谢谢!
  • 必须在VideoViewOnPreparedListener 中调用styleMediaController(myMC) 才能使其工作。否则 MediaController 视图没有子视图。
【解决方案2】:

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

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

【讨论】:

  • makeControllerViewprotected 对于我正在查看的来源。有人知道它是什么时候从private 变成protected 的吗?
  • 哦,也许您正在谈论 android.widget.MediaController 中的 makeControllerView 的 javadoc 中的 @hide。虽然这个 javadoc 位似乎阻止了 @Override 注释的工作,但它确实 not 实际上阻止了覆盖该方法。最初的问题几乎没有改变 - 这曾经是私人的吗?
【解决方案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);

必须在VideoViewOnPreparedListener 中调用styleMediaController(myMC) 才能使其工作。否则 MediaController 视图没有子视图。

【讨论】:

  • 什么是setCompatTint
猜你喜欢
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2012-08-11
  • 2015-06-23
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多