如何无需重写布局文件设置ExoPlayer的PlayerControlView背景透明?
如何不重写布局实现ExoPlayer PlayerControlView背景全透明?
当然可以,无需重写整个布局文件,以下是几种简单可行的实现方式:
1. XML布局中直接设置背景属性
直接给PlayerControlView添加透明背景属性即可,修改后的代码如下:
<com.google.android.exoplayer2.ui.PlayerControlView android:id="@+id/player_controls" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginBottom="16dp" android:background="@android:color/transparent" app:show_timeout="0" />
2. 代码动态设置背景
在Activity或Fragment中获取控件实例后,通过代码设置透明背景:
Kotlin示例:
val playerControls = findViewById<PlayerControlView>(R.id.player_controls) playerControls.background = ColorDrawable(Color.TRANSPARENT)
Java示例:
PlayerControlView playerControls = findViewById(R.id.player_controls); playerControls.setBackground(new ColorDrawable(Color.TRANSPARENT));
3. 通过主题样式全局/局部生效
如果希望所有PlayerControlView默认使用透明背景,可以在主题中配置样式:
<!-- 全局主题中添加 --> <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- 其他主题属性 --> <item name="playerControlViewStyle">@style/TransparentPlayerControlView</item> </style> <!-- 定义透明样式 --> <style name="TransparentPlayerControlView" parent="Widget.ExoPlayer.ControlView"> <item name="android:background">@android:color/transparent</item> </style>
注:如果设置后仍存在半透明遮罩,大概率是控件内部子View的背景导致,但绝大多数场景下,直接设置根控件的透明背景就能实现你参考图中控件悬浮在视频上方、无背景色块的效果。
内容的提问来源于stack exchange,提问作者Florian Walther
相关产品推荐
相关产品推荐

