android - 如何以编程方式为工具栏内的所有图标设置 colorControlHighlight (波纹颜色)?
问题描述
我知道有一种方法可以将主题设置为 xml 中的工具栏,如下所示:
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ToolBarTheme"/>
和风格:
<style name="ToolBarTheme">
<item name="android:colorControlHighlight">@color/white</item>
</style>
但是有没有办法以编程方式设置它或使用专门用于工具栏的主应用程序主题,而不仅仅是覆盖主主题中的 colorControlHighlight (因为它也会影响其他视图)?
解决方案
找到了适合我的解决方案。我有一个扩展 Toolbar 类的 CustomToolbar 类。在构造函数中,我将 ContextThemeWrapper 与我的工具栏主题一起使用:
class CustomToolBar : Toolbar {
constructor(context: Context?) : super(ContextThemeWrapper(context, R.style.ToolBarTheme))
constructor(context: Context?, attrs: AttributeSet?) : super(ContextThemeWrapper(context, R.style.ToolBarTheme), attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(ContextThemeWrapper(context, R.style.ToolBarTheme), attrs, defStyleAttr)
}
推荐阅读
- python - Mapbox 显示空白地图
- java - 从 QueryDSL 中的自定义表中选择
- xml - XSLT 删除同名节点中的节点
- docker - 挂载主机卷时无法重新启动Docker容器中的Jenkins
- java - 从可以采用多种形式并从 Java 中的 .properties 文件加载的字符串中提取值的最佳方法
- r - 循环遍历全局环境
- spring-boot - Spring Boot 教程,运行简单应用程序出错
- android - TypeError:null 不是对象(评估'RNGestureHandlerModule.default.Direction')
- javascript - 我可以自动将数据附加到链接吗?
- excel - 更改对列表框的引用以添加项目