
除了中規中矩的矩形按鈕外,5.0中將按鈕扁平化,產生了一個扁平按鈕——Flat Button。這個按鈕降低了很多存在感,主要用於在對話框,提示欄中。讓整個界面減少層級。今天說的就是它的用法。
這個按鈕繼承自矩形按鈕,所以擁有很多矩形按鈕的屬性,關於矩形按鈕請看上一篇文章。
首先還是添加lib的依賴。lib地址:https://github.com/shark0017/MaterialDesignLibrary
一、放入布局文件
自定義命名空間:xmlns:app="http://schemas.android.com/apk/res-auto"
<com.gc.materialdesign.views.ButtonFlat android:layout_width="wrap_content" android:layout_height="wrap_content" />
現在是默認的模式,沒有做任何設置。默認的狀況是完全透明的一個按鈕

二、在布局文件中設置各種屬性
android:background="@color/orange" 設置背景色,默認是透明的

android:text="Flat Button" 設置文字,默認沒有文字
android:textSize="20sp" 設置文字大小,默認14sp,文字是粗體。

android:textColor="#ffc641" 設置文字顏色,默認是藍色

app:rippleColor="#ffc641" 設置漣漪的顏色,默認是灰色
app:rippleSpeed="2" 設置漣漪擴散速度,默認6f

app:clickAfterRipple="false" 設置觸發click事件的時間,默認是漣漪擴散完了再出發點擊事件
三、通過代碼設定各種屬性
public class ButtonFlatTest extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button_flat); btn01.setBackgroundColor(getResources().getColor(R.color.orange));// 設定按鈕背景 btn02.setBackgroundColor(0xffff0000);// 設定按鈕背景 btn03.setText("setText");// 設定按鈕文字 btn04.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事件 buttonFlat.getTextView();// 得到這個按鈕中的textView對象,可以進行各種設定 } }
