2016-05-13 119 views
4

我有一個Android的按鈕,在這裏我想給它一個背景顏色和兩個連鎖反應的Android按鈕使用的背景顏色和漣漪效應

我紋波XML如下所示。

<?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="#BDA0CB" 
    tools:targetApi="lollipop"> 
    <item android:id="@android:id/mask"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#BDA0CB" /> 
     </shape> 
    </item> 
</ripple> 

和我的按鈕是

<Button android:id="@+id/Button_activity_login_ViewProfile" style="?android:textAppearanceSmall" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" android:text="Save" 
     android:textStyle="bold" 
     android:layout_marginLeft="@dimen/margin_left" 
     android:layout_marginRight="@dimen/margin_right" 
     android:textColor="#ffffffff" 
     android:fontFamily="sans-serif" 
     android:background="#ff7e51c2" /> 

爲了給連鎖反應我不得不刪除背景顏色...有沒有一種方法,我可以保留兩個。

+0

https://github.com/traex/RippleEffect –

回答

6

這可以通過在ripple.xml中添加android:drawable來實現。

首先將其添加到您的color.xml中。假設#ff7e51c2是您想要的按鈕的背景顏色。

... 
<color name="btn_bg_color">#ff7e51c2</color> 
.... 

然後創建按鈕的背景繪製(/drawable/btn_bg.xml):

<?xml version="1.0" encoding="utf-8"?><shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
<corners android:radius="2dp" /> 
<solid 
    android:color="@color/btn_bg_color" /> 
</shape> 

然後使用可繪製在ripple.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?android:colorControlHighlight"> 
    <item android:drawable="@drawable/btn_bg"/> 
</ripple> 
0

你可以在xml中做這樣的事情:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ripple"/> 

更改的drawable/文件夾ripple.xml如下:

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
        android:color="?android:colorControlHighlight"> 
    <item android:id="@android:id/mask"> 
     <shape android:shape="oval"> 
      <solid android:color="?android:colorAccent" /> 
     </shape> 
    </item> 

1

在繪製文件夾中創建ripple_effect.xml

<?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="#1182bc" 
    tools:targetApi="lollipop"> 
    <item android:id="@android:id/mask"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#1182bc" /> 
     </shape> 
    </item> 
</ripple> 

使用此XML作爲您的視圖的像下面

背景
android:background="@drawable/ripple_effect" 

希望這會有所幫助..

0

創建兩個不同版本的xml文件,以便它可以在Pre棒棒糖設備上工作。

抽拉/ button_effect.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#3ab5d6"/> 
    <corners android:radius="3dp"/> 
</shape> 

抽拉-V21/button_effect.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:tools="http://schemas.android.com/tools" 
    android:color="#635a5a" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    tools:targetApi="lollipop"> 
    <item android:drawable="@drawable/round_button" /> 
</ripple> 

在繪製文件夾中創建成形抽拉

drawable/round_button.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#3ab5d6"/> 
    <corners android:radius="3dp"/> 
</shape> 

你可以在佈局中做這樣的事情。xml:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_effect"/>