• Android Material Design之MaterialButtonToggleGroup(九)


    1. 效果图
      在这里插入图片描述
    2. 资源引入
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    
    • 1
    • 2
    1. 属性
    属性描述
    android:id控件Id
    android:layout_width控件长度
    android:layout_height控件高度
    app:checkedButton默认选中得按钮id
    app:selectionRequiredtrue 默认选择一个,不可不选,false 可不选
    app:singleSelectiontrue 单选, false 多选
    1. 源代码
      BottomAppBar.java
    package com.yyf.demo;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.content.ContextCompat;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.google.android.material.button.MaterialButtonToggleGroup;
    import com.yyf.demo.databinding.ActivityBottomAppBarBinding;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class BottomAppBar extends AppCompatActivity {
        private ActivityBottomAppBarBinding binding;
        private static final String TAG = "BottomAppBar";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = ActivityBottomAppBarBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
    
    
            binding.toggleButtonGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
                @Override
                public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                    switch (checkedId) {
                        case R.id.btn1:
                            Log.d(TAG, "onButtonChecked: 点击了btn1按钮 是否选中:" + isChecked);
                            break;
                        case R.id.btn2:
                            Log.d(TAG, "onButtonChecked: 点击了btn2按钮 是否选中:" + isChecked);
                            break;
                        case R.id.btn3:
                            Log.d(TAG, "onButtonChecked: 点击了btn3按钮 是否选中:" + isChecked);
                            break;
                    }
                }
            });
        }
    
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            Log.d(TAG, "onOptionsItemSelected: " + item.getItemId());
            switch (item.getItemId()) {
                case R.id.scan:
                    Log.d(TAG, "onOptionsItemSelected: " + item.getTitle());
                    break;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    activity_bottom_app_bar.xml

    
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/toggleButtonGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:checkedButton="@id/btn1"
            app:selectionRequired="true"
            app:singleSelection="false">
    
            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn1"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="aaa" />
    
            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn2"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="bbb" />
    
            <com.google.android.material.button.MaterialButton
                android:id="@+id/btn3"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ccc" />
        com.google.android.material.button.MaterialButtonToggleGroup>
    
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_gravity="bottom"
            app:backgroundTint="@color/black"
            app:hideOnScroll="true"
            app:menu="@menu/bab_menu" />
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/icon_scan"
            app:fabSize="normal"
            app:layout_anchor="@id/bottomAppBar" />
    
    androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    淘宝卖家如何批量采集竞品sku进行分析?推荐2个商品sku获取API
    当新手小白有了一块【香橙派OrangePi AIpro】.Demo
    C++ primer plus C++的编程模块(1)
    MySql InnoDB 存储引擎表优化
    Vite 详解
    VUE 打包后 动态修改 后台服务器地址
    减少软件故障、防范黑客攻击,软件质量安全问题不容忽视
    LT146 LRU
    java spring cloud 工程企业管理软件-综合型项目管理软件-工程系统源码
    【笔记】电商RFM模型
  • 原文地址:https://blog.csdn.net/csdn_yang123/article/details/128131817