如何使用drawerlayout
参考博客:http://blog.csdn.net/lmj623565791/article/details/41531475/
今天给简单的介绍下如何去使用DrawerLayout这个控件
首先是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/drawerlayout"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
tools:context="com.mag.administrator.drawerlayoutdemo.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/idlefish">
</RelativeLayout>
<fragment
android:id="@+id/left_menu"
android:name="com.mag.administrator.drawerlayoutdemo.MenuLeftFragment"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:tag="LEFT" />
</android.support.v4.widget.DrawerLayout>
这是MainActivity中的代码
private DrawerLayout drawerlayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图
initView();
//设置监听
setListener();
}
private void setListener() {
drawerlayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
//获取第一个子View,就是
View mContentView = drawerlayout.getChildAt(0);
float scale = 1 - slideOffset;
float rightScale = 0.8f + scale * 0.2f;
if (drawerView.getTag().equals("LEFT")) {
float leftScale = 1 - 0.3f * scale;
ViewHelper.setScaleX(drawerView, leftScale);
ViewHelper.setScaleY(drawerView, leftScale);
ViewHelper.setAlpha(drawerView, 0.6f + 0.4f * (1 - scale));
ViewHelper.setTranslationX(mContentView,
drawerView.getMeasuredWidth() * (1 - scale));
ViewHelper.setPivotX(mContentView, 0);
ViewHelper.setPivotY(mContentView,
mContentView.getMeasuredHeight() / 2);
mContentView.invalidate();
ViewHelper.setScaleX(mContentView, rightScale);
ViewHelper.setScaleY(mContentView, rightScale);
}
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
private void initView() {
drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
// 设置模式
drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
}
然后写上继承Fragment的类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.layout_menu, container, false);
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical">
<TextView
android:id="@+id/tv_one"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="itemone"
android:textSize="20sp" />
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="@id/tv_one"
android:layout_centerInParent="true"
android:gravity="center"
android:text="itemtwo"
android:textSize="20sp" />
</RelativeLayout>
这样就完成了。是不是很简单。