android学习之侧边菜单滑动,toolbar+DrawerLayout

android学习之侧边菜单滑动,toolbar+DrawerLayout

toolbar上篇有写,首先在xml文件中加入DrawerLayout,记住加上id

<android.support.v4.widget.DrawerLayout>
	android:id="@+id/drawer_layout"
......
</android.support.v4.widget.DrawerLayout>

DrawerLayout里面的内容需要写主内容区布局和侧边布局(或者选其一)(类似主内容写在了DrawerLayout布局中),内容区布局写在侧边布局前面;
另外控制侧边菜单从哪边滑出的:

android:layout_gravity="start"

实现DrawerLayout布局不覆盖住toolbar,需要toolbar写在DrawerLayout前,整个xml代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <!--toolbar-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7"
        android:background="@color/colorAccent"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme">

        <TextView
            android:layout_width=
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android实现顶部滑动菜单可以使用ViewPager和TabLayout配合使用,具体步骤如下: 1.在XML布局文件中添加ViewPager和TabLayout控件。 2.在Activity或Fragment中初始化ViewPager和TabLayout控件,并设置ViewPager的适配器。 3.创建Fragment类作为ViewPager的子页面,将需要展示的内容添加到Fragment的布局文件中。 4.在Activity或Fragment中设置TabLayout与ViewPager的联动,即使用TabLayout的setupWithViewPager()方法将TabLayout与ViewPager绑定。 5.在TabLayout中添加需要展示的菜单项,并设置TabLayout的样式。 6.在ViewPager的适配器中返回对应位置的Fragment实例。 7.在Fragment中设置需要展示的内容,例如ListView、RecyclerView等。 8.运行程序,查看效果。 示例代码: activity_main.xml ``` <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="?attr/actionBarTheme"/> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` MainActivity.java ``` public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); viewPager = findViewById(R.id.view_pager); setupViewPager(viewPager); tabLayout = findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new Fragment1(), "菜单1"); adapter.addFragment(new Fragment2(), "菜单2"); adapter.addFragment(new Fragment3(), "菜单3"); viewPager.setAdapter(adapter); } private static class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> fragmentList = new ArrayList<>(); private final List<String> fragmentTitleList = new ArrayList<>(); ViewPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); } @NonNull @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } void addFragment(Fragment fragment, String title) { fragmentList.add(fragment); fragmentTitleList.add(title); } @Nullable @Override public CharSequence getPageTitle(int position) { return fragmentTitleList.get(position); } } } ``` Fragment1.java ``` public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); // 设置需要展示的内容,例如ListView、RecyclerView等 return view; } } ``` 注意:以上代码中的布局文件和Fragment类仅作为示例,具体实现应根据需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值