Android关于侧边栏的简单使用(drawerLayout,navigationview,动态添加menu)

目前市面上大部分app都有使用侧边栏,假如的app还没有该功能,该让你的产品经理加班了哦。。
一、侧边栏一般包括header和menu,header一般包括背景图、用户名等一些东东,切图如下
效果图

下面贴出activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_left"
            android:textSize="18sp"
            android:text="联动"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:textColor="#ffffffff"/>
    </RelativeLayout>
    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
        <include layout="@layout/layout_my_fragment" />
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

2下面贴出在activity中的具体操作,其中包括动态添加navigation中的menu,及其点击事件;点击不同的menu在主页面中显示不同的fragment:

public class Main2Activity extends FragmentActivity {

    @Bind(R.id.nav_view)
    NavigationView navView;
    @Bind(R.id.tv_left)
    TextView tvLeft;
    @Bind(R.id.drawer)
    DrawerLayout drawer;

    private List<TaskSceneFragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);

        navView.setItemIconTintList(null);//此处是设置menu图标的颜色为图标本身的颜色
        navView.getMenu().add(1, 1, 1, "menu_1");//动态添加menu
        navView.getMenu().add(2, 2, 2, "menu_1");
        navView.getMenu().add(3, 3, 3, "menu_1");
        //菜单的点击事件
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getOrder()) {
                    case 1:
                        switchFragment(fragments.get(0));
                        Toast.makeText(Main2Activity.this, "点击menu1", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        switchFragment(fragments.get(1));
                        Toast.makeText(Main2Activity.this, "点击menu2", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        switchFragment(fragments.get(2));
                        Toast.makeText(Main2Activity.this, "点击menu3", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });

        System.out.println("navHeaderCount:" + navView.getHeaderCount());
        navView.getHeaderView(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main2Activity.this, "click_head", Toast.LENGTH_SHORT).show();
            }
        });

        initFragment();


    }

    private void switchFragment(TaskSceneFragment fragment) {
        if (fragment.isAdded()) {
            fm.beginTransaction().hide(currentFragment).show(fragment).commit();
        } else {
            fm.beginTransaction().add(R.id.frame_layout, fragment).hide(currentFragment).show(fragment).commit();
        }
        currentFragment = fragment;
    }

    FragmentManager fm;
    TaskSceneFragment currentFragment;

    private void initFragment() {
        fm = getSupportFragmentManager();
        for (int i = 0; i < 3; i++) {
            TaskSceneFragment fragment = TaskSceneFragment.newInstance(i, "");
            fragments.add(fragment);
        }
        fm.beginTransaction().add(R.id.frame_layout, fragments.get(0)).commit();
        currentFragment = fragments.get(0);
    }

    @OnClick(R.id.tv_left)
    public void onClick() {
        //此处是点击联动按钮打开或者关闭侧边栏
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }else {
            drawer.openDrawer(GravityCompat.START);
        }
    }
}

简单实现侧边栏就是这么的简单,欢迎大家积极留言,点赞,您的鼓励会给予我非常大的动力,非常感谢!!!!!。下一篇将会给大家带来Fragment的嵌套使用,还是基于当前demo,欢迎大家积极指正错误。。

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Android浮动侧边栏可以提供一个快捷方式,方便用户在应用快速访问功能或页面。下面是一个简单的实现方法: 1. 在布局文件添加浮动按钮,可以使用FloatingActionButton组件。 2. 添加一个DrawerLayout布局,作为侧边栏的容器。 3. 在DrawerLayout添加NavigationView布局,用于显示侧边栏内容。 4. 在Activity设置浮动按钮的点击事件,当点击按钮时,打开侧边栏。 5. 在NavigationView添加菜单项,用于实现各种功能或跳转页面。 下面是一个示例代码: ``` // 布局文件 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加浮动按钮 --> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_margin="16dp" android:src="@drawable/ic_add_white_24dp" /> </RelativeLayout> <!-- 添加侧边栏 --> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start"> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_item_1" android:title="菜单项1" /> <item android:id="@+id/nav_item_2" android:title="菜单项2" /> <item android:id="@+id/nav_item_3" android:title="菜单项3" /> </group> </menu> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> // Activity代码 public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private NavigationView mNavigationView; private FloatingActionButton mFab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = findViewById(R.id.drawer_layout); mNavigationView = findViewById(R.id.navigation_view); mFab = findViewById(R.id.fab); // 设置浮动按钮点击事件 mFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mDrawerLayout.openDrawer(Gravity.START); } }); // 设置侧边栏菜单项点击事件 mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.nav_item_1: // 菜单项1点击事件 break; case R.id.nav_item_2: // 菜单项2点击事件 break; case R.id.nav_item_3: // 菜单项3点击事件 break; } // 关闭侧边栏 mDrawerLayout.closeDrawer(Gravity.START); return true; } }); } } ``` 这样就可以实现一个简单Android浮动侧边栏,用户可以方便地访问应用的功能或页面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不羁的闰土

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

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

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

打赏作者

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

抵扣说明:

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

余额充值