MaterialDesign 的動畫特铝,3D,的效果非常好;
但是也有蛋疼的地方苟呐,比如痒芝,自動把所有的按鈕,加上了動畫牵素,懸浮效果;
動畫也就算了澄者,懸浮效果一加笆呆; app的風格會比較亂;
下面是修改Button的懸浮粱挡,和動畫效果顏色的介紹,還有自定義 水波紋效果赠幕;
左邊的是默認Button效果,右邊是我改了顏色的询筏;
如果你需要MD風格的水波紋效果榕堰,那就不能給Button加background;
加了之后嫌套,只有3D的懸浮懸浮效果了逆屡;
如果你要改變Button的默認背景顏色,和按下去的水波紋顏色踱讨,
只需要這么做
<!-- style代碼-->
<style name="AppTheme.Button" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/white</item>//按鈕默認顏色
<item name="colorControlHighlight">@color/red</item>//按鈕按下的水波紋改變顏色
</style>
<!--Button的代碼-->
<Button
android:id="@+id/app_dialog_buttonleft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"
android:theme="@style/AppTheme.Button" />
<!--注意使用【主題】而不是style-->
引入MD后魏蔗,會默認給所有的Button加上懸浮效果,如果你不需要怎么辦痹筛?
那么需要這樣:
<Button
android:id="@+id/app_dialog_buttonleft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:text="Button"
android:theme="@style/AppTheme.Button" />
android:background="?android:attr/selectableItemBackground"
//如果你是用的v7,寫成
android:background="莺治?attr/selectableItemBackground"
這就是去掉了懸浮的效果的button
MD的水波紋效果很好,但是只有按鈕上面有帚稠,你可能不會滿足谣旁,
你很可能需要在 LinearLayout,RelativeLayout滋早,textview等View使用
那么你可以這樣
通過引入原生的Style的方式使用榄审,當然這種方式的壞處是,背景默認是透明的馆衔,好處是瘟判,自己不需要做兼容;
如果需要需要背景色角溃,那么文章后面的自定義
<style name="AppLayoutTheme" parent="AppTheme">
//這里更改水波紋顏色
<item name="colorControlHighlight">@color/red</item>
</style>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:theme="@style/AppLayoutTheme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點這里看線性布局的水波紋效果"/>
</LinearLayout>
示例:
這里需要注意一點:
不過是上面的Button和(LinearLayout)布局拷获,如果使用了這個水波紋效果
android:background="?attr/selectableItemBackground"
那么他是無法設置默認的顏色的;
通過看源碼减细,可以知道他的默認效果是透明的
這是源碼:
----------------------自定義------------------
如果需要自己自定義效果 比如背景色需要自定義匆瓜,圓角需要自定義,那么手擼一個,不用系統(tǒng)的
在res目錄建立一個drawable-v21的文件夾(因為水波紋效果的屬性驮吱,只能在5.0+才能使用)茧妒;
如果你的App需要兼容到5.0以下,就需要在默認的 drawabe目錄建立一個一樣的文件(test_md.xml) 左冬;
不然app會在5.0以下的手機出錯;
--------------文件名------test_md.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
//水波紋顏色
android:color="@color/colorPrimary"
//點擊中心點的大小桐筏,不建議使用,不然在6.0+上面會導致拇砰,點擊哪里 水波紋都是在中心
android:radius="5dp">
//給item設置系統(tǒng)maskId梅忌,限制水波紋不越界,
<item android:id="@android:id/mask">
//如果需要改變使用水波紋控件的圓角除破,需要用到shape
<shape android:shape="rectangle">
//圓角度
<corners android:radius="8dp" />
//默認的背景顏色
<solid android:color="?android:attr/colorAccent" />
</shape>
</item>
</ripple>
<TextView
android:id="@+id/adi_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/test_md"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="刪除并退出討論組"
android:textColor="@color/white"
android:textSize="14sp" />
Look Image
這里是使用TextView牧氮,帶有水波紋效果,但是沒有懸浮效果瑰枫,如果需要懸浮效果的話踱葛, 那么
<TextView
android:id="@+id/adi_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/test_md"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="刪除并退出討論組"
android:textColor="@color/white"
android:theme="@style/AppTheme.Button"
android:textSize="14sp" />
//繼承兼容包的Button樣式,就會有懸浮效果了
<style name="AppTheme.Button" parent="Widget.AppCompat.Button">
</style>
能完成此效果光坝,在這里要感謝 QJar的指點尸诽;
QJar的博客地址 http://blog.csdn.net/qJay_Dev