矩形按鈕是我們很常用的控件,讓其擁有5.0動效必須自定義控件。本文講解的控件是參考:
https://github.com/shark0017/MaterialDesignLibrary
一、放入布局文件
我們在添加這view的時候需要寫一個命名空間
xmlns:materialdesign="http://schemas.android.com/apk/res-auto
<com.gc.materialdesign.views.ButtonRectangle android:layout_width="wrap_content" android:layout_height="wrap_content""/>
這個就是最原始的控件了,我們參照了官方控件的尺寸,定義了最小的寬高是:w=80,h=36
這個控件在eclipse中是看不到具體樣子的,我雖然已經盡可能把代碼寫的較為可視化了,但由於是由lib導入的關系,所以還是沒辦進行編譯器中實時展示。不過真正運行出來的時候樣子是完全正常的啦。下面來看看實際運行出來的樣子。
二、在布局文件中進行設置各種屬性
默認的屬性值
android:background="#1E88E5" 設置按鈕的背景色,默認是藍色
android:text="" 設置按鈕上的文字,默認無文字
android:textSize="12sp" 設置按鈕上文字的大小,默認12(反正不是12就是14,總之就是textView的默認文字大小)
android:textColor="#ffffff" 設置按鈕文字的顏色,默認是白色
app:rippleColor="#xxxxxx" 設置按鈕漣漪的顏色,默認是根據背景色來動態設置的。如果你設定了漣漪顏色,那么就不會根據背景色來動態產生漣漪顏色。
app:rippleSpeed="5.5f" 漣漪的擴散速度5.5f,數組越大越快。你可以看見如果速度為2的話擴散的很慢
app:clickAfterRipple="true" 是否在漣漪擴散完后再觸發點擊事件,默認是漣漪消失后才出發點擊事件
三、通過代碼進行設定各種屬性值
package com.example.hhh; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; import com.gc.materialdesign.views.ButtonRectangle; public class ButtonRectangleTest extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button_rectangle); ButtonRectangle btn = (ButtonRectangle)findViewById(R.id.button); btn.setBackgroundColor(getResources().getColor(R.color.orange));// 設定按鈕背景 btn.setBackgroundColor(0xffff0000);// 設定按鈕背景 btn.setText("代碼:setText");// 自定義文字 btn.setTextSize(30);// 自定義文字大小 btn05.setTextColor(0xffffc641);// 自定義文字顏色 btn06.setTextColor(getResources().getColor(R.color.red));// 通過資源定義文字顏色 btn07.setRippleColor(0xffffc641);// 設置漣漪顏色 btn08.setRippleColor(getResources().getColor(R.color.orange));// 通過資源設定漣漪顏色 btn09.setRippleSpeed(2);// 漣漪擴散的速度 btn10.setClickAfterRipple(false);// 點擊后立刻響應click事件,不管漣漪是否擴散完畢 } }