2016-09-13 28 views
1

我想創建一個自定義形狀,這將在印刷機上的反應就像主題「Base.Widget.AppCompat.Button.Colored」按鈕。改變形狀,而mantaining其材質設計連鎖反應

然而bacause我有自定義其形狀(圓角我必須覆蓋其android:background - 這是迄今爲止我所知道的唯一的方式(不......不,我不會用骯髒的黑客與的FrameLayout)。 目前,它可以在XML文件中提供我們的定製<shape>作爲背景繪製完成

最有前途的代碼,這使selectableItemBackground - 如此項重要的對我來說,就是:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <solid android:color="@color/colorAccent"/> 
      <corners android:topLeftRadius="@dimen/button_shape_radius" 
       android:topRightRadius="@dimen/button_shape_radius"/> 
     </shape> 
    </item> 

    <item android:drawable="?attr/selectableItemBackground"> 
     <shape> 
      <solid/> 
      <corners android:topLeftRadius="@dimen/button_shape_radius" 
       android:topRightRadius="@dimen/button_shape_radius"/> 
     </shape> 
    </item> 
</layer-list> 

不幸的是我不能用<item android:drawable="?attr/selectableItemBackground">來塑造第二個項目,因此最終壓制的項目的形狀是矩形。 enter image description here

我會appreaciate如果有人會給我這個問題的解決方案。 我使用API​​_MIN = 16,所以不能使用漣漪效應。我也不想使用FrameLayout或外部庫,這迫使我用Button來包裝Button。

回答

1

要低於21紋波可以使用這個庫Ripple Effect你用你的形狀,而不紋波標籤和環繞視圖與RippleView瞭解更多詳情,您可以檢查Usage部分

21+你可以試試這個我沒有測試它,但它應該工作,把這個文件夾繪製-V21

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
android:color="?attr/colorControlHighlight"> 
<item> 
    <shape> 
     <solid android:color="@color/colorAccent"/> 
     <corners android:topLeftRadius="@dimen/button_shape_radius" 
      android:topRightRadius="@dimen/button_shape_radius"/> 
    </shape> 
</item> 

<item android:drawable="?attr/selectableItemBackground"> 
    <shape> 
     <solid/> 
     <corners android:topLeftRadius="@dimen/button_shape_radius" 
      android:topRightRadius="@dimen/button_shape_radius"/> 
    </shape> 
</item> 
</ripple> 

UPDATE

我改變d漣漪效應的一個按鈕

的源代碼,你可以得到的代碼here

用法

就像任何普通的鈕釦

XML

<path.to.the.RippleButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_rounded_top_blue" 
    android:id="@+id/mybutton"/> 

的Java

RippleButton button = (RippleButton) findViewById(R.id.mybutton); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(MainScreen.this, "You did it", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

如果需要其他視圖,您只需要進行一些調整,以源代碼很容易(在我看來)

+0

庫的作品,但它是不是最好的soultion 4me,因爲我有在我的視圖外使用每一次包裝,而我想聲明**一** **代碼重用的目的的風格 – murt

+0

你是對的,我試圖做同樣的事情昨天,我有我的應用程序的4個意見填滿屏幕,我必須用'Rippleview'包裝每一個,我在幾個設備上測試過,我沒有性能問題,但我的佈局很簡單 –

+0

我沒有批准你的答案,因爲我希望有人想出了更好的東西 – murt