MaterialDesign之Slider+SnackBar+FloatingActionButton

Slider的使用

詳情參考

  • 添加依賴:
  implementation 'com.google.android.material:material:1.2.1'
  • 若要使控件正常顯示,則對(duì)應(yīng)AppTheme應(yīng)該修改為:
<resources>
    <!-- Base application theme.修改為如下主題 -->
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>
  • 布局中引入Slider以及屬性說明
  <com.google.android.material.slider.Slider
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:valueFrom="0f"
        android:valueTo="100f"
        android:value="20f"

        app:haloColor="@color/color1"
        app:haloRadius="20dp"

        app:labelBehavior="withinBounds"

        app:thumbColor="@color/color2"
        app:thumbRadius="10dp"
        app:thumbElevation="10dp"

        app:trackColorActive="@color/color3"
        app:trackColorInactive="@color/color4"

        android:stepSize="1"
        />
  • android:valueFrom:滑動(dòng)起始點(diǎn)

  • android:valueTo:滑動(dòng)終點(diǎn)

  • android:value:滑動(dòng)起始值設(shè)定

  • app:haloColor:拖動(dòng)時(shí)出現(xiàn)的圓形顏色

  • app:haloRadius:拖動(dòng)時(shí)大圓的半徑

  • app:labelBehavior:滑動(dòng)時(shí)數(shù)值提示顯示()

  • app:thumbColor:滑塊的顏色設(shè)置

  • app:thumbRadius:滑塊的半徑

  • app:thumbElevation:滑塊陰影

  • app:trackColorActive:已經(jīng)拖動(dòng)的區(qū)域顏色

  • app:trackColorInactive:未覆蓋的區(qū)域

  • android:stepSize:步進(jìn)設(shè)置

  • 常用方法調(diào)用和數(shù)值監(jiān)聽

 private void initSlider(){
        custom_slider = findViewById(R.id.slider_custom);
        custom_slider.addOnChangeListener(new Slider.OnChangeListener() {
            @Override
            public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
              //此處可獲取相關(guān)滑動(dòng)數(shù)值  
            }
        });
   }
  • 效果圖如下:


    result1.gif

SnackBar的使用

  • 確定好顯示的父布局,直接加載代碼即可
 private void showSnackBar(){
        LinearLayout activity_content = findViewById(R.id.activity_content);
        Snackbar.make(activity_content,"標(biāo)題", Snackbar.LENGTH_LONG)
                .setAction("點(diǎn)擊事件", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"Toast",Toast.LENGTH_SHORT).show();
                    }
                }).setDuration(Snackbar.LENGTH_LONG).show();
    }
  • 顯示效果如下:


    result2.gif

FloatingActionButton的使用

  • 布局屬性解析
   <com.google.android.material.floatingactionbutton.FloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentRight="true"
       android:layout_margin="20dp"
       android:layout_alignParentEnd="true"
       android:src="@drawable/ic_mic_black_24dp"
       android:clickable="true"
       app:backgroundTint="@color/color_normal"
       android:elevation="3dp"
       app:pressedTranslationZ="6dp"
       app:fabSize="normal"
       android:focusable="true"
       app:rippleColor="@color/color_pressed"
      />
  • app:backgroundTint:按鈕背景顏色

  • android:elevation:陰影高度,該屬性使得控件有陰影,像浮起來一樣

  • app:pressedTranslationZ:按下的陰影顯示

  • app:fabSize:按鈕大小,兩種取值:normal、mini

  • app:rippleColor:按下波紋顯示

  • 事件監(jiān)聽與Button一致,所以不再贅述,效果圖如下


    result3.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容