赞
踩
- <android.support.v4.widget.DrawerLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/id_drawerlayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <FrameLayout
- android:id="@+id/id_framelayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#fffaf0" >
- </FrameLayout>
-
- <LinearLayout
- android:id="@+id/id_linearlayout"
- android:layout_width="200dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:background="#f5f5f5"
- android:orientation="vertical" >
-
- <ListView
- android:id="@+id/id_listView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:divider="#deb887"
- android:dividerHeight="1dp" >
- </ListView>
- </LinearLayout>
- </android.support.v4.widget.DrawerLayout >
这里直接放一个drawerlayout,里面上面那个是主内容
下面的是侧滑菜单,这里放的一个ListView
下面放java文件
- package com.example.sliding;
-
-
-
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
-
-
- public class MainActivity extends ActionBarActivity implements OnItemClickListener {
- private DrawerLayout mDrawLayout;
- private ListView mListView;
- private FragmentManager fragmentManager;
- private Fragment fragment1,fragment2,fragment3;
- private String[] str;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- intiView();
- }
- private void intiView() {
- mDrawLayout=(DrawerLayout) findViewById(R.id.id_drawerlayout);
- mListView=(ListView) findViewById(R.id.id_listView);
- str = new String[] { "item1", "item2", "item3"};
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.
- R.layout.simple_list_item_1, str);
- mListView.setAdapter(adapter);
- mListView.setOnItemClickListener(this);
- fragmentManager = getSupportFragmentManager();
- fragment1= new Fragment1();
- fragment2 = new Fragment2();
- fragment3 = new Fragment3();
-
- fragmentManager.beginTransaction().replace(R.id.id_framelayout, fragment1).commit();
- setTitle("fragment 1");
- mDrawLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
- });
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- switch (position) {
- case 0:
- fragmentManager.beginTransaction().
- replace(R.id.id_framelayout, fragment1).commit();
- setTitle("fragment1");
- break;
- case 1:
- fragmentManager.beginTransaction().
- replace(R.id.id_framelayout, fragment2).commit();
- setTitle("fragment2");
- break;
- case 2:
- fragmentManager.beginTransaction().
- replace(R.id.id_framelayout, fragment3).commit();
- setTitle("fragment3");
- break;
- }
- mDrawLayout.closeDrawers();
- }
-
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。