布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/v4_drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/v4_drawerlayout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/v4_text"
android:textSize="22sp"
android:textColor="@color/colorAccent"
android:gravity="center"
/>
</FrameLayout>
<ListView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="@+id/v4_listview"
android:choiceMode="singleChoice"
android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>
代码区
public class DrawerActivity extends AppCompatActivity {
private ListView listView;
private DrawerLayout drawerLayout;
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_activity);
initView();
}
private void initView()
{
listView=(ListView) findViewById(R.id.v4_listview);
drawerLayout=(DrawerLayout) findViewById(R.id.v4_drawerlayout);
textView=(TextView) findViewById(R.id.v4_text);
initDate();
}
private void initDate(){
final List<String> list = new ArrayList<String>();
list.add("网易");
list.add("腾讯");
list.add("新浪");
list.add("搜狐");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView.setText(list.get(position));
showDrawerLayout();
}
});
drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开 不设置则不会默认打开
}
private void showDrawerLayout() {
if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.openDrawer(Gravity.LEFT);
} else {
drawerLayout.closeDrawer(Gravity.LEFT);
}
}
}