Android底部导航栏

Android底部导航栏

今天简单写了一个底部导航栏,封装了一个库,用法比较简单

效果图

Android底部导航栏

Github地址:https://github.com/kongqw/KqwBottomNavigation

导入库

Android底部导航栏

Android底部导航栏

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.kongqw.bottomnavigationlib.KqwBottomNavigation
        android:id="@+id/kbn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

使用

package com.kongqw.kqwbottomnavigation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.kongqw.bottomnavigationlib.KqwBottomNavigation;
import com.kongqw.bottomnavigationlib.OnBottomNavigationSelectedListener;
import com.kongqw.bottomnavigationlib.ToastUtil;

public class MainActivity extends AppCompatActivity implements OnBottomNavigationSelectedListener {

    private KqwBottomNavigation mKqwBottomNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mKqwBottomNavigation = (KqwBottomNavigation) findViewById(R.id.kbn);

        mKqwBottomNavigation.setBottomNavigationSelectedListener(this);
    }

    @Override
    public void onValueSelected(int index) {
        ToastUtil.show(this, "index = " + index);
    }
}

API

  • 添加底部按钮点击的监听
public void setBottomNavigationSelectedListener(OnBottomNavigationSelectedListener l)

点击后的回调如下

@Override
public void onValueSelected(int index) {
    ToastUtil.show(this, "index = " + index);
}
  • 设置底部栏某个功能为选中状态
public void setBottomNavigationClick(int index)

我这里为了方便,只设定了0~3,依次代表底部四个按钮,可以使用枚举,只允许传入特定的值

上一篇:去除Android 6.0 界面下的导航栏:NavigationBar


下一篇:PreferenceActivity使用示例