【问题标题】:Can not add ripple effect on TextView using foreground selectableItemBackground无法使用前景 selectableItemBackground 在 TextView 上添加波纹效果
【发布时间】:2018-04-08 09:07:01
【问题描述】:

我尝试在 TextView 上添加波纹效果,用作深色背景的按钮。

我已经试过了

  1. android:foreground="?attr/selectableItemBackground" 在 TextView 上
  2. android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" 在 TextView 父级上,因为 Textview 有深色背景,也尝试了浅色主题。

我也无法将 TextView 背景设置为 android:background="attr/selectableItemBackground,因为我需要在 textview 上设置一些深色背景。

谁能给我理解,我怎样才能达到这个要求。

【问题讨论】:

    标签: android android-layout textview android-button


    【解决方案1】:

    问题在于深色主题。波纹颜色在黑暗中不可见。所以你应该尝试使用自定义主题。

      <style name="TextViewTheme" parent="@style/Base.ThemeOverlay.AppCompat.Dark">
        <!-- Customize your theme here. -->
        <item name="colorControlHighlight">@color/colorAccent</item>
    </style>
    

    为 TextView 添加这个主题。

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_green_dark"
        android:foreground="?android:selectableItemBackground"
        android:text="Android"
        android:theme="@style/TextViewTheme"
        android:clickable="true"
        android:textSize="23sp"/>
    

    【讨论】:

    • 哥们还是看不到涟漪效应
    • 你放了 Click listener 吗?因为我已经对其进行了测试并且可以正常工作。
    • 是的,我使用这个文本视图作为按钮
    • 顺便说一句,当我将 selectableitembackground 作为 textview 的背景时它的工作方式,但在这种情况下,我无法设置我的自定义背景颜色
    • 它也适用于Button。阅读答案。我没有把selectableitembackground 当作background 我使用了一种颜色作为背景。
    【解决方案2】:

    可以像这样使用MaterialButton 来对点击产生连锁反应。

        <com.google.android.material.button.MaterialButton
            style="?attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="0dp"
            android:minHeight="0dp"
            android:paddingHorizontal="@dimen/padding_8dp"
            android:text="Clickable Text"
            android:textColor="your text color"
            app:backgroundTint="@android:color/transparent"
            app:rippleColor="ripple effect color" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-24
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多