效果:

可进行侧滑,点击可切换不同页面


对Toolbar的设置,添加三个菜单选项,并给菜单选项设置点击事件

public class DrawerLayoutUsing extends AppCompatActivity {    private DrawerLayout mDrawerLayout;    private ListView mDrawerList;    ArrayList<String> menuList;    private ArrayAdapter<String> adapter;    private Toolbar toolbar;    private ActionBarDrawerToggle mToggle;    private FragmentManager fragmentManager;    private ListView mylist;    private LinearLayout ll_drawer;    private int currentFragmentId = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        setTitle("讨论专区");//        设置菜单内容        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        mDrawerList = (ListView) findViewById(R.id.lv_drawer);        menuList = new ArrayList<>();        menuList.add("热门帖子");        menuList.add("品牌讨论");        menuList.add("我要发贴");        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menuList);        mDrawerList.setAdapter(adapter);        ll_drawer = (LinearLayout) findViewById(R.id.ll_drawer);//        设置点击事件        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {                switchFragement(position);            }        });//        设置toolbar        toolbar = findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);//        设置左上标(箭头)        getSupportActionBar().setHomeButtonEnabled(true);        getSupportActionBar().setDisplayHomeAsUpEnabled(true);        mToggle.syncState();        mDrawerLayout.addDrawerListener(mToggle);//        设置默认fragment        fragmentManager = getSupportFragmentManager();        fragmentManager.beginTransaction().add(R.id.ll_content,new Fragment1()).commit();    }//switchFragment方法切换界面    private void switchFragement(int fragmentId) {        mDrawerLayout.closeDrawer(ll_drawer);//        if(currentFragmentId==fragmentId&&fragmentId!=2){//            return;//        }//        currentFragmentId=fragmentId;        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        switch (fragmentId){            case 0:                Intent intentFragment1 = new Intent(this,BannerActivity.class);                startActivity(intentFragment1);                fragmentTransaction.replace(R.id.ll_content,new Fragment1());                toolbar.setTitle("讨论专区");                break;            case 1:                Intent intentFragment2 = new Intent(this,ListViewActivity.class);                startActivity(intentFragment2);                fragmentTransaction.replace(R.id.ll_content, new Fragment1());                toolbar.setTitle("讨论专区");                break;            case 2:                Intent intentFragment3 = new Intent(this,AddArticles.class);                startActivity(intentFragment3);                fragmentTransaction.replace(R.id.ll_content, new Fragment1());                toolbar.setTitle("讨论专区");                break;        }        fragmentTransaction.commit();    }

三个Fragment切换三个不同区的界面:

三个类似只展示其中一个

public class Fragment1 extends Fragment {    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){        return inflater.inflate(R.layout.fragment1,container,false);    }}

activity_main.xml:

添加两个布局包含在其中

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:fitsSystemWindows="true"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <include layout="@layout/mytoolbar"/>    <include layout="@layout/drawlayout1"/></LinearLayout>

mytoolbar.xml:

设置ToorBar

<?xml version="1.0" encoding="utf-8"?><androidx.appcompat.widget.Toolbar 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="wrap_content"    android:id="@+id/toolbar"    android:background="?attr/colorPrimary"    android:minHeight="?attr/actionBarSize"    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"    ></androidx.appcompat.widget.Toolbar>

drawlayout1.xml:

设置菜单项样式

<androidx.drawerlayout.widget.DrawerLayout    android:id="@+id/drawer_layout"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!-- 主界面-->    <LinearLayout        android:id="@+id/ll_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">    </LinearLayout>    <LinearLayout        android:id="@+id/ll_drawer"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:layout_gravity="start">    <!-- 导航视图-->    <ListView        android:layout_width="240dp"        android:layout_height="match_parent"        android:id="@+id/lv_drawer"        android:background="#FFFFFF"        android:choiceMode="singleChoice"        android:divider="@color/transparent"        android:dividerHeight="0dp"        android:clickable="true"/>    </LinearLayout></androidx.drawerlayout.widget.DrawerLayout>

对ActionBar进行修改

<style name="AppTheme.NoActionBar">    <item name="windowActionBar">false</item>    <item name="windowNoTitle">true</item></style>

Android自定义ActionBar报错

https://blog.csdn.net/weixin_44223180/article/details/107372143


文章部分技术参考:https://blog.csdn.net/miyuexingchen/article/details/52232751?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1

更多相关文章

  1. android音乐播放器(4)
  2. Android仿苹果关机界面实现代码
  3. 读取Android联系人列表
  4. Android(安卓)8.1 锁屏界面 壁纸半透明改为全透明
  5. Android实现截屏的方法
  6. android ListView GridView 单选和复选列子
  7. Android(安卓)实例教程
  8. 简单安卓QQ登录界面
  9. 弹窗样式dialog

随机推荐

  1. [Android]Toolbar使用详解(三)——源码解析
  2. RK3288[android 7.1]调试笔记 去掉桌面上
  3. android静态图片和动态壁纸
  4. Android(安卓)Studio 自动检查依赖库是否
  5. android-apt plugin is incompatible wit
  6. Android中关于cpu/cpuset/schedtune的应
  7. The differences between @+id and @andr
  8. Android使用Intent启动Service的Intent必
  9. PhoneGap Android hello android:minSdkV
  10. 进程保活