I have a rounded rectangle with elevation that casts a shadow, just like in the example here: http://developer.android.com/preview/material/views-shadows.html#shadows
我有一個圓形的矩形,它的高程投射陰影,就像這里的例子:http://developer.android.com/preview/material/views-shadows.html#陰影。
Here is my shape:
這是我的形狀:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="6dp" />
</shape>
I want to get the nice "ripple" touch effect that everything else has, but when that's set as the view's background, no touch feedback is given. The shape stays white.
我想要得到所有其他東西都具有的“波紋”觸摸效果,但是當它被設置為視圖的背景時,就不會給出觸摸反饋。形狀保持白色。
So, I made it into a layer-list
:
所以,我把它做成了一個層次列表:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="6dp" />
</shape>
</item>
<item android:drawable="?android:selectableItemBackground" />
</layer-list>
Now I get nice touch feedback when I tap it, but there's a problem.
當我點擊它時,我得到了很好的觸摸反饋,但是有一個問題。
The outline of the rounded rectangle shape was lost. It still draws the white rounded rectangle fine, but the shadow is cast as a non-rounded rectangle (square edges), and the ripple goes all the way out past the corners:
圓角矩形的輪廓丟失了。它仍然把白色圓角矩形畫得很好,但是陰影被投射成一個非圓角矩形(正方形的邊緣),漣漪一直延伸到角落:
It doesn't look too horrible on here, but on the device it's pretty ugly and off-putting.
它在這里看起來不太可怕,但在設備上它是相當丑陋和令人討厭的。
Is there a way to fix this? The Outline
section of the first link seems to be what I want, but I can't figure out how to implement it.
有辦法解決這個問題嗎?第一個鏈接的Outline部分似乎是我想要的,但是我不知道如何實現它。
64
Try that one:
試試這個:
ripple.xml
ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/COLOR_WHILE_PRESSING" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background"></item>
</ripple>
background.xml
background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/BACKGROUND_COLOR" />
<corners android:radius="6dp" />
</shape>
Or maybe this post will help you: Android L FAB Button shadow
或者這篇文章可以幫助你:Android L FAB按鈕陰影
I explained there, how to implement the new FAB button, I also used the outline for that.
我在那里解釋了,如何實現新的FAB按鈕,我也使用了大綱。
2
click here for complete source code
點擊這里獲得完整的源代碼
Create button.xml in drawable folder
創建按鈕。xml在可拉的文件夾
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark"/>
<item android:drawable="@color/colorPrimary"/>
</selector>
Create button.xml in drawable-v21 folder
創建按鈕。xml drawable-v21文件夾中
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@color/colorPrimary" />
</ripple>
you can set button.xml as background of your view.
你可以設置按鈕。xml作為視圖的背景。
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/button"
android:clickable="true"
android:gravity="center"
android:padding="16dp"
android:text="Android Ripple Effect Custom"
android:textColor="#fff"
android:textSize="18sp" />
if you want custom color for ripple effect instead of default gray, then you can archive it by adding colorControlHighlight in your style.
如果您想要為紋波效果定制顏色,而不是默認的灰色,那么您可以通過在樣式中添加colorControlHighlight來存檔它。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
</style>
</resources>
1
ripple touch effect button in android studio
android studio中的紋波觸摸效果按鈕
<com.xgc1986.ripplebutton.widget.RippleButton
android:id="@+id/Summit"
android:layout_width="260dp"
android:layout_height="50dp"
android:text="Login"
android:textColor="@color/white"
android:textStyle="bold"
app:buttonColor="@color/Button"
app:rippleColor="@color/white" />
For more reference click here http://androiddhina.blogspot.in/2015/04/android-ripple-touch-effect-example.html
更多的參考點請點擊此處http://androiddhina.blogspot.in/2015/04/android-ripple-touch- example.html。
1
<RelativeLayout
android:foreground="?android:attr/selectableItemBackground">
</RelativeLayout>
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/07/02/c06041e3a0d9d76a0399423c7fb6858f.html。