赞
踩
第一步 对应的主布局
- <?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:id="@+id/v4_drawerlayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.samsung.ceshi.DaohanglanActivity">
-
- <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>
接下来对应的就是activity
- public class DaohanglanActivity extends AppCompatActivity {
-
-
- private ListView listView;
-
- private DrawerLayout drawerLayout;
-
- private TextView textView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.daohanglan);
-
- 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));
- if(list.get(position).equals("网易"))
- {
- Intent intent = new Intent(DaohanglanActivity.this,Main2Activity.class);
- startActivity(intent);
- }
- if(list.get(position).equals("腾讯"))
- {
- Intent intent = new Intent(DaohanglanActivity.this,MainActivity.class);
- startActivity(intent);
- }
- showDrawerLayout();
- }
- });
-
- drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开 不设置则不会默认打开
- }
-
- private void showDrawerLayout() {
- if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
- drawerLayout.openDrawer(Gravity.LEFT);
- } else {
- drawerLayout.closeDrawer(Gravity.LEFT);
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。