Android侧拉,抽屉
核心方法: drawer_layout.openDrawer(left_drawer);
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="侧拉"/>
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#33333333"
android:dividerHeight="0dp"
android:background="#ffeeeeee"
android:scrollbars="none"/>
</android.support.v4.widget.DrawerLayout>
主页面代码:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer_layout;
private ListView left_drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer_layout = findViewById(R.id.drawer_layout);
left_drawer = findViewById(R.id.left_drawer);
// drawer_layout.openDrawer(left_drawer);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer_layout.openDrawer(left_drawer);
}
});
}
}