2016-07-12 38 views
6

向android按鈕添加顏色後,會失去其漣漪效果,使用戶感覺有點響應。我該如何解決?我已經通過許多解決方案進行了搜索,但是我找不到明確的解決方案。Android,設置按鈕的背景色失去漣漪效應

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".ClockInOutFragment"> 


    <AnalogClock 
     android:id="@+id/clock" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/date_and_time"/> 


    <RelativeLayout 
     android:id="@+id/date_and_time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"> 

     <TextView 
      android:id="@+id/time_digits" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="12:10" 
      android:textSize="45sp"/> 

     <TextView 
      android:id="@+id/am_pm" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/time_digits" 
      android:layout_toRightOf="@+id/time_digits" 
      android:paddingLeft="3dp" 
      android:text="PM" 
      android:textSize="20sp"/> 

     <TextView 
      android:id="@+id/date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/time_digits" 
      android:text="Mon, July 11" 
      android:textSize="22sp"/> 
    </RelativeLayout> 

    <!--Clock in and out buttons--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/textView3" 
      android:layout_width="0dp" 
      android:layout_height="56dp" 
      android:layout_weight="1" 
      android:background="#4CAF50" 
      android:gravity="center" 
      android:text="Clock In" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#FFFFFF"/> 

     <!--Divider between the clock in and out button--> 
     <View 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="#B6B6B6"/> 

     <Button 
      android:id="@+id/textView4" 
      android:layout_width="0dp" 
      android:layout_height="56dp" 
      android:layout_weight="1" 
      android:background="#FF5252" 
      android:gravity="center" 
      android:text="Clock Out" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#FFFFFF"/> 
    </LinearLayout> 


</RelativeLayout> 

回答

9

可以與其它附加的紋波繪製添加的連鎖反應&背景色:

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button_connect" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dip" 
     android:fontFamily="sans-serif" 
     android:text="Connect" 
     android:background="@drawable/ripple" 
     android:textColor="#FFFFFF" 
     android:textSize="18sp" /> 

</LinearLayout> 

ripple.xml(這是你除了可以添加背景顏色到漣漪效應):

<?xml version="1.0" encoding="utf-8"?> 
<!-- in drawable folder--> 
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?android:colorControlHighlight"> 

    <item android:id="@android:id/mask"> 
     <shape android:shape="rectangle"> 
      <solid android:color="?android:colorAccent" /> 
     </shape> 
    </item> 

    <item> 
     <shape android:shape="rectangle"> 
      <!-- put your background color here--> 
      <solid android:color="@color/default_color" /> 
     </shape> 
    </item> 

</ripple> 
+0

如何區分哪一個是面具和哪一個是默認顏色? –

7

不要更改按鈕背景。更改主題。

<style name="ButtonGray"> 
    <item name="colorButtonNormal">@color/gray</item> 
</style> 

,並在XML文件中

<Button 
    android:id="@+id/accept_button" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:text="@string/button_accept_group" 
    android:theme="@style/ButtonGray"/> 

或者你可以在主應用主題

<style name="AppTheme" 
      parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorButtonNormal">@color/primary_color</item> 
</style> 

添加它,並不需要改變按鈕的背景。

如果你想完全自定義背景,你需要創建你的選擇器。你可以在那裏設置連鎖效應。

0

只需使用:

android:backgroundTint="#4CAF50" 

相反的:

android:background="#4CAF50" 

不要忘記改變這樣做是爲了你的Buttonandroid.support.v7.widget.AppCompatButton

+0

我確實改變了,但不起作用 –

1

一個非常簡單和直接的方式將您的按鈕的屬性設置爲?attr/selectableItemBackgroundandroid:foreground。下面的xml是完全有效的和作品

<Button 
    android:id="@+id/btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:foreground="?attr/selectableItemBackground"/>