【问题标题】:Android ripple background colorAndroid 波纹背景颜色
【发布时间】:2015-08-13 22:02:08
【问题描述】:

我在导航抽屉上使用了涟漪效果。我已将其设置为这样并将其应用于我的 ListView:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/black_200" />

    <item android:state_pressed="true" android:color="@color/black_200">
        <ripple android:color="@color/black_400" />
    </item>

    <item android:drawable="@color/white" />
</selector>

我希望波纹背景保持与正常激活状态相同。即使我将颜色定义为与激活状态相同,它也会变得更暗,并且波纹气泡变得更暗。如何将背景着色为与激活状态相同的颜色,并将波纹气泡着色为我告诉它的颜色?

【问题讨论】:

    标签: android android-activity android-listview navigation-drawer rippledrawable


    【解决方案1】:

    使用样式要容易得多:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       ...
       <item name="colorControlHighlight">@color/ripple_material_dark</item>
       ...
    </style>
    

    【讨论】:

    • 作为已接受答案的发布者,我投票将其作为已接受答案
    • Xamarin.Android 中也能发挥魅力,当然没有惊喜!
    • 简单且最佳的解决方案
    • 我不明白你的解决方案。您是否建议在单击元素时更改样式值?
    【解决方案2】:

    您可以通过执行以下操作来控制RippleDrawable 的颜色:

    RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable
    
    int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} };
    int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue
    
    ColorStateList colorStateList = new ColorStateList(states, colors);
    rippleDrawable.setColor(colorStateList);
    

    或者,通过 XML:

    <?xml version="1.0" encoding="utf-8"?>
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#FFFF0000"> <!-- The ripple will be red -->
    
        <!-- the normal bg color will be light grey -->
        <item>
            <color android:color="#FFDDDDDD" />
        </item>
    
        <!-- make sure the ripple doesn't exceed the bounds -->
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="?android:colorAccent" />
            </shape>
        </item>
    </ripple>
    

    编辑

    在看到下面@i.shadrin 的回答后,我必须承认这是一种更简单的方法(使用样式)。如果这是您的选择,我会推荐它。

    【讨论】:

    • 在xml中也可以吗?
    • 谢谢。我如何从我现有的选择器链接到这个涟漪?
    • 我认为你可以复制整个涟漪元素并替换你的涟漪元素。
    • 你也可以用这个替换你的drawable xml文件,并在这个xml中进行必要的调整。这真的是一个玩弄它并看看最适合您需求的问题。
    • 你能分享你的后向 API 的 xml 解决方案(在 api 版本 21 之前)吗?
    【解决方案3】:

    在您的视图中添加这两行代码以产生涟漪效果。

    android:clickable="true"
    android:background="?attr/selectableItemBackground"
    

    【讨论】:

      【解决方案4】:

      如果您使用自定义按钮颜色,请使用以下代码来获得涟漪效果。 将文件保存在名称为 my_ripple.xml 的 drawable 中 并设置在按钮的背景中。

       <?xml version="1.0" encoding="utf-8"?>
       <ripple xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:color="@color/ripple_color"
          tools:targetApi="lollipop">
          <item android:id="@android:id/mask">
           <shape android:shape="rectangle">
              <solid android:color="@color/ripple_color"
                  />
          </shape>
        </item>
        <item
          android:id="@android:id/background"
          android:drawable="@color/button_color" />
       </ripple>
      

      【讨论】:

        【解决方案5】:

        在 api lvl 21 及更高版本上与选择器一起使用的示例波纹

        <?xml version="1.0" encoding="utf-8"?>
        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
                android:color="?attr/colorControlHighlight">
        
            <!-- ripple mask -->
            <item android:id="@+id/mask">
                <color android:color="@android:color/white"/>
            </item>
        
            <!-- background shape or color -->
            <item android:drawable="@drawable/rectangle_rounded_steel_blue"/>
        
        </ripple>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多