[英]How to have padding for ripple effect
以前,我使用StateDrawable
來實現帶有陰影效果的自定義卡片視圖 - https://stackoverflow.com/a/33829309/72437 。 這是由於CardView
的限制,無法在其cardBackgroundColor
接受選擇器
現在,我想添加漣漪效應。 我使用以下 XML。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#00ff00">
<item android:drawable="@drawable/statelist_item_background"/>
</ripple>
然而,這會產生副作用,即綠色波紋一直延伸到觸及陰影區域。
我想避免接觸陰影區域的綠色波紋。 我嘗試添加填充信息。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#00ff00">
<item>
<shape android:shape="rectangle">
<padding android:top="10dp" android:right="10dp" android:bottom="10dp" android:left="10dp" />
</shape>
</item>
<item android:drawable="@drawable/statelist_item_background"/>
</ripple>
然而,它沒有什么不同。 在ripple
標簽中添加填充信息沒有區別。
我可以知道如何填充波紋效果嗎?
您可以使用inset
標簽來定義您需要的填充。 有一個inset
屬性來定義全局填充或每個方向的 4 個屬性。
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="10dp">
<ripple
android:color="#00ff00">
<item android:drawable="@drawable/statelist_item_background"/>
</ripple>
</inset>
首先創建可繪制文件(例如:bg_ripple.xml)
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="4dp">
<ripple android:color="@color/gray_light">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<!-- this color does not matter, it's a mask shape -->
<solid android:color="@color/blue" />
<corners android:radius="@dimen/button_corner_radius" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="@dimen/button_corner_radius" />
<stroke
android:width="2dp"
android:color="@color/red"
/>
<solid android:color="@color/white" />
</shape>
</item>
</ripple>
</inset>
其次將它作為背景添加到按鈕
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click_me"
android:background="@drawable/bg_ripple"
/>
您應該將此添加到您的 CarView:
<CardView
...
style="@style/MyCardViewStyle"
android:foreground="?attr/selectableItemBackground">
</CardView>
然后,在你的風格中添加:
<style name="MyCardViewStyle">
<item name="colorControlHighlight">@color/my_awesome_color</item>
</style>
它將為您的漣漪效果提供您想要的令人敬畏的顏色,而無需自己創建漣漪,並且會自動檢測邊界,因此您無需查找是否可以為漣漪添加填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.