1:布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开抽屉" />
</LinearLayout>
<!--这是侧边栏滑动时出现的右边布局内容-->
<LinearLayout
android:id="@+id/drawer_content"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="end" 抽屉向左展示则改为start即可
android:clickable="true"
android:orientation="vertical"
android:background="#fff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="嗯嗯"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="喜喜"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/processed_commit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="完成"
android:textSize="16sp" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
2:主要代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout mDrawerLayout;
private LinearLayout drawer_content;
private GridView stateGV;
private GridView timeGV;
private TextView filterTV;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer_content = (LinearLayout) findViewById(R.id.drawer_content);
filterTV = (TextView) findViewById(R.id.processed_commit);
btn = (Button) findViewById(R.id.btn_drawer);
filterTV.setOnClickListener(this);
btn.setOnClickListener(this);
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
@Override
public void onDrawerClosed(View drawerView) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.processed_commit:
mDrawerLayout.closeDrawers();
break;
case R.id.btn_drawer:
mDrawerLayout.openDrawer(drawer_content);
break;
}
}