【发布时间】:2018-10-22 16:27:22
【问题描述】:
我在一个 Android 应用中自定义了我的标签,使其具有图标而不是文本。但是,文本标签在选择时会突出显示。我已经通过 OnTabSelectedListener 解决了这个问题,其中设置了一个更亮的图标。但是我仍然不知道如何在从一个页面滑动到另一个页面的一半以上时实现突出显示的原生效果。
有什么建议吗?提前致谢。
【问题讨论】:
标签: android tabs swipe highlight
我在一个 Android 应用中自定义了我的标签,使其具有图标而不是文本。但是,文本标签在选择时会突出显示。我已经通过 OnTabSelectedListener 解决了这个问题,其中设置了一个更亮的图标。但是我仍然不知道如何在从一个页面滑动到另一个页面的一半以上时实现突出显示的原生效果。
有什么建议吗?提前致谢。
【问题讨论】:
标签: android tabs swipe highlight
而不是使用OnTabSelectedListener,您应该使用根据是否被选中而自行更改的图标。
如果您的图标是矢量绘图,那么您可以使用 颜色状态列表 自动更改它们的颜色。以下是铅笔图标的外观:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#0000ff"/>
<item android:color="#aaaaaa"/>
</selector>
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/blue_when_selected"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25z
M20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>
现在,无论您在哪里设置标签图标,如果您使用 R.drawable.ic_pencil,您将看到自动颜色切换。
如果您的图标是 .png 文件,那么您可以使用 State list drawable 来达到相同的效果。假设你有res/drawable-mdpi/icon_normal.png 和res/drawable-mdpi/icon_selected.png。然后你可以有这个:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/icon_selected"/>
<item
android:drawable="@drawable/icon_normal"/>
</selector>
同样,如果您使用R.drawable.icon,您会看到您可以在两个 .png 文件之间自动切换。
【讨论】: