Android实战项目(一) 仿微信底部导航栏

仿微信底部导航栏

要做一个类似微信底部导航栏要用到 MaterialDesign系列之 BottomNavigationView,找图片的话推荐一个很好用的网站https://www.iconfont.cn。
实现效果:页面跳转,触摸图标颜色变化。顶部Toolbar设置,
退出效果。
项目源码下面会有,持续更新中。
在这里插入图片描述

  1. 添加依赖
	//BottomNavigationView
    implementation 'com.google.android.material:material:1.2.1'
  1. xml布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">

    <include layout="@layout/toolbar_layout"/>

    <com.example.client.widget.NormalRefreshView
        android:id="@+id/refreshableView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_alignParentBottom="true"
            app:itemIconTint="@drawable/bottom_navigation_item_selector"
            app:itemTextColor="@drawable/bottom_navigation_item_selector"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/main_bottom_navigation" />
    </com.example.client.widget.NormalRefreshView>
</LinearLayout>
  1. 主界面设置 MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener{

    private List<Fragment> mFragments = new ArrayList<>();

    @BindView(R.id.bottomNavigationView)
    BottomNavigationView mNavigationView;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.message)
    TextView text_message;

    private RelativeLayout layout_message;
    private RelativeLayout layout_contacts;
    private RelativeLayout layout_find;
    private RelativeLayout layout_mine;

    /**
     * 退出时间
     */
    private long exitTime;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mFragments.add(new MessageFragment());
        mFragments.add(new ContactsFragment());
        mFragments.add(new FindFragment());
        mFragments.add(new MineFragment());

    @Override
    protected void initWidget() {
        super.initWidget();
        layout_message=findViewById(R.id.layout_message);
        layout_contacts=findViewById(R.id.layout_contacts);
        layout_find=findViewById(R.id.layout_find);
        layout_mine=findViewById(R.id.layout_mine);


        // 初始化页卡
        initPager();
        //设置当前界面
//        viewPager.setCurrentItem(2);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SUCCESS:
//                    refreshableView.finishRefresh(true);
                    normalRefreshView.finishRefresh(true);
                    text_message.setText("信息");
                    break;
                case FAILED:
//                    refreshableView.finishRefresh(false);
                    normalRefreshView.finishRefresh(false);
                    break;
                default:
                    break;
            }
        };
    };

    private void initPager() {
        FragmentAdapter adapter = new FragmentAdapter(mFragments, getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        //BottomNavigationView 点击事件监听
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int menuId = menuItem.getItemId();
                // 跳转指定页面:Fragment
                switch (menuId) {
                    case R.id.menu_message:
                        viewPager.setCurrentItem(0,false);
                        break;
                    case R.id.menu_contacts:
                        viewPager.setCurrentItem(1,false);
                        break;
                    case R.id.menu_find:
                        viewPager.setCurrentItem(2,false);
                        break;
                    case R.id.menu_mine:
                        viewPager.setCurrentItem(3,false);
                        break;
                }
                return false;
            }
        });

        // ViewPager 滑动事件监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //将滑动到的页面对应的 menu 设置为选中状态
                mNavigationView.getMenu().getItem(position).setChecked(true);
                if(position==0){
                    layout_message.setVisibility(View.VISIBLE);
                    layout_contacts.setVisibility(View.GONE);
                    layout_find.setVisibility(View.GONE);
                    layout_mine.setVisibility(View.GONE);
                }if(position==1){
                    layout_message.setVisibility(View.GONE);
                    layout_contacts.setVisibility(View.VISIBLE);
                    layout_find.setVisibility(View.GONE);
                    layout_mine.setVisibility(View.GONE);
                } if(position==2){
                    layout_message.setVisibility(View.GONE);
                    layout_contacts.setVisibility(View.GONE);
                    layout_find.setVisibility(View.VISIBLE);
                    layout_mine.setVisibility(View.GONE);
                }if(position==3) {
                    layout_message.setVisibility(View.GONE);
                    layout_contacts.setVisibility(View.GONE);
                    layout_find.setVisibility(View.GONE);
                    layout_mine.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });

    }
    
    //退出功能
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {
            // 重写键盘事件分发,onKeyDown方法某些情况下捕获不到,只能在这里写
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Snackbar snackbar = Snackbar.make(viewPager, "再按一次退出程序", Snackbar.LENGTH_SHORT);
                snackbar.getView().setBackgroundResource(R.color.colorPrimary);
                snackbar.show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
    
}
  1. 添加4个碎片 大体一样这里列举其中一个
    MessageFragment
public class MessageFragment extends BaseFragment {
    @Override
    protected int getLayoutId() {
        return R.layout.message_fragement;
    }
}

message_fragement.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="消息"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:layout_marginTop="300dp"
        android:layout_gravity="center" />
</LinearLayout>
  1. 要在res文件,新建一个menu文件夹
    新建main_bottom_navigation
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_message"
        android:enabled="true"
        android:icon="@mipmap/sel_message"
        android:title="消息"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_contacts"
        android:enabled="true"
        android:icon="@mipmap/sel_contacts"
        android:title="通讯录"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_find"
        android:enabled="true"
        android:icon="@mipmap/sel_find"
        android:title="发现"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/menu_mine"
        android:enabled="true"
        android:icon="@mipmap/sel_mine"
        android:title=""
        app:showAsAction="ifRoom" />
</menu>
  1. Toolbar这里我是自定义的 列举其中一个

新建一个toolbar_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@color/black"
    android:layout_height="wrap_content">

    <include layout="@layout/message_toolbar"
        android:visibility="visible"/>

    <include layout="@layout/contacts_toolbar"
        android:visibility="gone"/>

    <include layout="@layout/find_toolbar"
        android:visibility="gone"/>

    <include layout="@layout/mine_toolbar"
        android:visibility="gone"/>

</LinearLayout>

新建一个message_toolbar文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="15dp"
        android:text="消息"
        android:gravity="center_vertical"
        android:minHeight="45dp"
        android:textSize="17sp"
        android:textColor="@color/white"/>

    <ImageView
        android:id="@+id/message_add"
        android:layout_width="23dp"
        android:layout_height="23dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/find_add"
        android:layout_marginRight="15dp"/>
</RelativeLayout>
  1. 触摸图标时候颜色变化
    在drawable新建bottom_navigation_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_focused" android:state_checked="true" />
    <item android:color="@color/color_un_focused" android:state_checked="false" />
</selector>

在之前主界面activity_main中添加,app:labelVisibilityMode="labeled"图标超过3个显示文字,不然会隐藏。

		app:itemIconTint="@drawable/bottom_navigation_item_selector"
        app:itemTextColor="@drawable/bottom_navigation_item_selector"
        app:labelVisibilityMode="labeled"
  1. 项目地址

下面是我的项目源码地址,持续更新。
https://github.com/hazardhhh/Myclient.git

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在 Android 中实现类似微信底部状态栏的效果,可以通过使用 Dialog 来实现。下面是一个简单的示例: 1. 首先,在布局文件中创建一个包含底部状态栏的自定义布局(例如在底部有几个按钮和文本显示的布局)。 2. 在代码中创建一个自定义的 Dialog,使用上述布局。 3. 设置 Dialog 的样式,使其显示在屏幕的底部。 4. 使用 Dialog 的 show() 方法显示对话框。 例如: 1. 创建一个名为 dialog_bottom_status.xml 的布局文件,包含底部状态栏的按钮和文本。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="vertical"> <Button android:id="@+id/btn_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="状态按钮" /> <TextView android:id="@+id/text_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="状态文本" /> </LinearLayout> ``` 2. 创建一个名为 BottomStatusDialog 的类来实现自定义 Dialog。 ```java public class BottomStatusDialog extends Dialog { public BottomStatusDialog(Context context) { super(context); setContentView(R.layout.dialog_bottom_status); Button btnStatus = findViewById(R.id.btn_status); TextView textStatus = findViewById(R.id.text_status); // 在这里可以设置底部按钮和文本的点击事件和内容 // 设置对话框样式为底部显示 Window window = getWindow(); if (window != null) { window.setGravity(Gravity.BOTTOM); window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } } } ``` 3. 在需要显示底部状态栏的地方调用 BottomStatusDialog 的 show() 方法来显示对话框。 ```java BottomStatusDialog dialog = new BottomStatusDialog(this); dialog.show(); ``` 通过上述步骤,就可以实现一个类似微信底部状态栏的 Dialog。根据实际需求,你可以在布局文件和对话框类中添加其他的按钮和功能以满足你的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十壹、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值