效果图
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" tools:context="com.example.lenovo.mydrawerlayout.MainActivity"> <androID.support.v4.Widget.DrawerLayout androID:layout_wIDth="match_parent" androID:ID="@+ID/drawer_layout" androID:layout_height="match_parent"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="这是主界面" /> </linearLayout> <linearLayout androID:layout_wIDth="300dp" androID:layout_height="match_parent" androID:layout_gravity="left" androID:background="#f00" > <TextVIEw androID:ID="@+ID/t" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="这是侧拉界面" /> </linearLayout> </androID.support.v4.Widget.DrawerLayout> </linearLayout>
注:DrawerLayout下有两个布局,DrawerLayout的第一个子元素是主要内容,即抽屉没有打开时显示的布局,DrawerLayout的第二个子元素是抽屉中的内容,即抽屉布局,layout_gravity为侧拉方向
因为用到Toolbar所以要把Actionbar替换了所以设置
values下设置styles.xml 为NoActionbar
<resources> <!-- Base application theme. --> <style name="Apptheme" parent="theme.AppCompat.light.NoActionbar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="Apptheme.NoActionbar"> <item name="windowActionbar">false</item> <item name="windowNoTitle">true</item> </style> <style name="Apptheme.AppbarOverlay" parent="themeOverlay.AppCompat.Dark.Actionbar" /> <style name="Apptheme.PopupOverlay" parent="themeOverlay.AppCompat.light" /> </resources>
MainActivity
package com.example.lenovo.mydrawerlayout; import androID.os.Bundle; import androID.support.design.Widget.Snackbar; import androID.support.v4.vIEw.GravityCompat; import androID.support.v4.Widget.DrawerLayout; import androID.support.v7.app.ActionbarDrawerToggle; import androID.support.v7.app.AppCompatActivity; import androID.support.v7.Widget.Toolbar; import androID.vIEw.VIEw; import androID.Widget.TextVIEw; public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //Toolbar代替Actionbar Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); // DrawerLayout DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); //Toolbar上面最左边显示三杠图标监听DrawerLayout ActionbarDrawerToggle toggle = new ActionbarDrawerToggle( this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); //侧拉页面字体 TextVIEw t= (TextVIEw) findVIEwByID(R.ID.t); t.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Snackbar.make(v,"侧拉页面",Snackbar.LENGTH_LONG) .setAction("Action",null).show(); } }); } @OverrIDe public voID onBackpressed() { DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackpressed(); } } }总结
以上是内存溢出为你收集整理的Android DrawerLayout实现侧拉菜单功能全部内容,希望文章能够帮你解决Android DrawerLayout实现侧拉菜单功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)