drawerLayout其实是一个布局控件,跟LinearLayout等控件是一样的,但是drawerLayout带有滑动的功能。只要按照drawerLayout的规定布局方式写完布局,就能有侧滑的效果。
1)在DrawerLayout中,第一个子View必须是显示内容的view,并且设置它的layout_width和layout_height属性是match_parent.
2)第二个view是抽屉view,并且设置属性layout_gravity="left|right",表示是从左边滑出还是右边滑出。设置它的layout_height="match_parent"
ActionBarDrawerToggle就是DrawerLayout事件的监听器。
ActionBarDrawerToggle有3个方法可以被复写,分别用来实现DrawerLayout打开,关闭,滑动的事件监听:
drawerLayout布局代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
>
//主内容
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/drawer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"/>
</FrameLayout>
//侧滑菜单
<ListView
android:id="@+id/left_drawer"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
mainactivity.java代码
package com.example.wxj.drawerlayoutlearen;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private TextView view;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
view = (TextView)findViewById(R.id.drawer_text);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.open,
R.string.close
){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
view.setText("close");
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
view.setText("dakai");
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
view.setText("huachu");
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
string.xml代码
<resources>
<string name="app_name">DrawerLayoutlearen</string>
<string name="open" />
<string name="close" />
</resources>
您可能感兴趣的文章:Android手机显示多彩霓虹灯效果Android Shader应用开发之霓虹闪烁文字效果Android图像处理之霓虹滤镜效果Android布局之帧布局FrameLayout详解Android布局之FrameLayout帧布局Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控Android布局实现圆角边框效果Android动画效果之自定义ViewGroup添加布局动画(五)Android给布局、控件加阴影效果的示例代码Android编程实现圆角边框布局效果的方法Android开发实现布局帧布局霓虹灯效果示例