Android Material Design之MaterialButtonToggleGroup(九)

  1. 效果图
    在这里插入图片描述
  2. 资源引入
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
  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);
    }
}

activity_bottom_app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值