• Android Material Design之BottomAppBar(八)


    1.上图

    1. 资源引入
    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:backgroundTint控件背景颜色
    app:hideOnScroll滚动时隐藏
    app:menu菜单选项
    1. 源代码
      activity_bootom_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">
    
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    
        <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

    BootomAppBarActivity.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.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";
        private List<String> list = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = ActivityBottomAppBarBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
    
    
            for (int i = 0; i < 100; i++) {
                list.add(String.valueOf(i));
            }
    
            binding.recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
            binding.recyclerView.setAdapter(new RecyclerView.Adapter() {
                @NonNull
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
                    Holder holder = new Holder(view);
                    return holder;
                }
    
                @Override
                public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                    Holder holder1 = (Holder) holder;
                    TextView textView = holder1.itemView.findViewById(R.id.tv_item);
                    if (position % 2 == 0) {
                        textView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.purple_500));
                    } else {
                        textView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.purple_700));
                    }
                    textView.setText(list.get(position));
                }
    
    
                @Override
                public int getItemCount() {
                    return list.size();
                }
            });
        }
    
        private static class Holder extends RecyclerView.ViewHolder {
            public Holder(@NonNull View itemView) {
                super(itemView);
            }
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu, menu);
            return true;
        }
    
        @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
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    menu.xml

    
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/more"
            android:icon="@drawable/ic_baseline_more_vert_24"
            android:title="更多"
            app:showAsAction="always">
            <menu>
                <item
                    android:id="@+id/scan"
                    android:title="扫一扫" />
            menu>
        item>
    menu>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 注意事项
      想要实现FloatingActionButton 嵌入BottomAppBar中得效果,必须使用CoordinatorLayout包裹,在FloatingActionButton中使用锚点app:layout_anchor=“引入BottomAppBar得控件Id”
  • 相关阅读:
    < Python全景系列-5 > 解锁Python并发编程:多线程和多进程的神秘面纱揭晓
    ERC20通证标准是什么?
    【Unity3D】获取UGUI位置不正确问题
    公链之Sui(前脸书/Meta员工成立的新公链项目)
    王学岗音视频开发(一)—————配置NDK开发环境
    Flutter 项目实战 注册接口实现https协议访问(三)
    阿里的 24W 字 Java 面试复盘指南!
    毕业设计项目选题Java高考志愿咨询平台 高考志愿填报助手系统源码+调试+开题+lw
    华为云云耀云服务器L实例评测|在 Centos & Docker 中使用Nginx部署Vue项目
    Qt通过正则表达式筛选出字符串中的手机号
  • 原文地址:https://blog.csdn.net/csdn_yang123/article/details/128129443