如何修改Android tensileHighlightTopImage与tensileHighlightBottomImage颜色?求示例
修改tensileHighlightTopImage和tensileHighlightBottomImage颜色的实现示例
一、静态XML主题配置方式
- 在
res/values/styles.xml中定义自定义主题,重写对应属性:
<style name="CustomScrollTheme" parent="Theme.AppCompat.Light"> <!-- 替换顶部拉伸高亮图 --> <item name="android:tensileHighlightTopImage">@drawable/tint_tensile_highlight</item> <!-- 替换底部拉伸高亮图 --> <item name="android:tensileHighlightBottomImage">@drawable/tint_tensile_highlight</item> </style>
- 创建
res/drawable/tint_tensile_highlight.xml,通过tint设置自定义颜色:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@android:drawable/overscroll_glow" android:tint="#FF6200EE" /> <!-- 此处替换为你的目标颜色值 -->
- 给滚动控件应用该主题:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:theme="@style/CustomScrollTheme" />
二、代码动态修改方式(运行时调整)
Kotlin 示例
val scrollView = findViewById<ScrollView>(R.id.target_scroll_view) // 获取顶部拉伸高亮Drawable val topHighlight = scrollView.getDrawable(android.R.attr.tensileHighlightTopImage) // 获取底部拉伸高亮Drawable val bottomHighlight = scrollView.getDrawable(android.R.attr.tensileHighlightBottomImage) // 设置自定义颜色 topHighlight?.setTint(ContextCompat.getColor(this, R.color.your_custom_color)) bottomHighlight?.setTint(ContextCompat.getColor(this, R.color.your_custom_color))
Java 示例
ScrollView scrollView = findViewById(R.id.target_scroll_view); Drawable topHighlight = scrollView.getDrawable(android.R.attr.tensileHighlightTopImage); Drawable bottomHighlight = scrollView.getDrawable(android.R.attr.tensileHighlightBottomImage); if (topHighlight != null) { topHighlight.setTint(ContextCompat.getColor(this, R.color.your_custom_color)); } if (bottomHighlight != null) { bottomHighlight.setTint(ContextCompat.getColor(this, R.color.your_custom_color)); }
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

