此博客转自 https://blog.csdn.net/mhhyoucom/article/details/80102887
DrawerLayout 模仿QQ 测滑菜单
布局文件:
<?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:layout_height="match_parent"
android:id="@+id/mainLayout"
tools:context="com.chengxing.comchenxingnetapp.MainActivity">
<RelativeLayout
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/show"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/main_fl_drawer"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="0dp">
<TextView
android:background="@color/colorPrimaryDark"
android:text="@string/app_name"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
代码文件:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@PNViewFind
DrawerLayout mainLayout;
@PNViewFind
RelativeLayout main_fl_drawer;
@PNViewFind
RelativeLayout right;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PNViewFind.Bind.init(this);
mainLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
//获取屏幕的宽高
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
//设置右面的布局位置 根据左面菜单的right作为右面布局的left 左面的right+屏幕的宽度(或者right的宽度这里是相等的)为右面布局的right
right.layout(main_fl_drawer.getRight(), 0, main_fl_drawer.getRight() + display.getWidth(), display.getHeight());
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
});
}
@Override
public void onClick(View v) {
mainLayout.openDrawer(main_fl_drawer);
}
}