DrawerLayout是一种Material Design风格的侧滑菜单,v4包里提供了该控件,可以轻松实现侧滑效果,通常与ToolbarNavigation控件搭配。

实现效果:

 Android开发丶MaterialDesign实战のDrawerLayout

实现步骤:

1.XML文件中,把最外层布局改成DrawerLayout

<android.support.v4.widget.DrawerLayout>

*******

*******

*******

*******

*******

*******

</android.support.v4.widget.DrawerLayout>

2.编辑弹出菜单的布局,我这里设置了一个线性布局LinearLayout,里面放了一个TextView

<LinearLayout

android:id= “@+id/main_ll”

android:width= “match_parent”

android:height= “match_parent”

android: 0background= “@color/primaryColor”

android:oritention= “vertical”>

<TextView

android:id=”@+id/main_ll_tv”

android:width=”wrap_content”

android:height=”wrap_content”

android:text=”Fantasychong

android:textsize=”16sp”

/>

</LinearLayout>

运行效果:

 Android开发丶MaterialDesign实战のDrawerLayout

这时候点击右边的空白区域并不能关闭菜单,而且没有阴影影响观感,在xml文件中加一层FrameLayout即可解决这个问题。

<FrameLayout

android:width=”match_parent”

android:height=”match_parent”

>

</FrameLayout>

运行效果:

 Android开发丶MaterialDesign实战のDrawerLayout

这时发现侧滑菜单没有覆盖顶部的导航栏,影响观感,于是决定去掉系统默认的导航栏,而使用ToolBar

1.打开style.xml文件,先将系统主题AppTheme改为NoActionBar

2.xml文件中刚才添加的FrameLayout中设置Toolbar

<FrameLayout

android:width= “match_parent”

android:height= “match_parent”>

<android.support.v7.widget.Toolbar

android:id=”main_toolbar”

android:width= “match_parent”

android:height= “match_parent”

android:theme= “@style/ThemeOverlay.AppCompat.Dark.ActionBar”

app:popupTheme=”@style/ThemeOverlay.AppCompat.Light”>

</android.support.v7.widget.Toolbar>

</FrameLayout>

3.打开目标Activity,初始化ToolBar

protected void onCreate(Bundle savedInstance){

super.onCreate(savedInstance);

setContentView(R.layout.activity_main);

toolbar=(Toolbar) findViewById(R.id.main_toolbar);

setSupportActionBar(toolbar);

}

运行结果:

 Android开发丶MaterialDesign实战のDrawerLayout


Demo下载

http://download.csdn.net/download/u014078990/9960507

点击打开链接

相关文章:

  • 2021-08-06
  • 2021-07-30
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-06-09
  • 2021-04-27
  • 2021-12-04
猜你喜欢
  • 2021-11-09
  • 2021-05-23
  • 2021-05-18
  • 2021-12-16
  • 2021-08-23
  • 2022-01-09
  • 2021-08-06
相关资源
相似解决方案