android 多个activity侧滑栏的实现

来源:互联网 发布:缺陷检测算法 编辑:程序博客网 时间:2024/06/05 09:20
最近项目需要做侧滑栏来实现菜单,时间比较紧,所以就从网上找了很多代码看,刚开始想用slidingMenu 这个去做,虽然功能比较多但是发现用起来很麻烦,而我是极其不喜欢麻烦的人,于是找了官方API,发现了 DrawerLayout这个东西,用起来方便,也适合在多个activity中用。废话不说了,下面讲解代码:首先是main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".DrawerActivity" >        <android.support.v4.widget.DrawerLayout          android:id="@+id/drawer_layout"          android:layout_width="match_parent"          android:layout_height="match_parent" >            <!-- 主界面视图 -->          <RelativeLayout              android:id="@+id/content"              android:layout_width="match_parent"               android:layout_height="match_parent" >              <include                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  layout="@layout/mainui_activity" />  <!-- 主界面布局可以放在这里引用-->        </RelativeLayout>          <!-- 侧面的视图 -->          <RelativeLayout              android:id="@+id/left"              android:layout_width="240dp"                     android:layout_height="match_parent"              android:background="@android:color/transparent"            android:layout_gravity="left" >                <include                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  layout="@layout/leftlayout_drawer" />  <!-- 左侧栏放在这里引用-->        </RelativeLayout>      </android.support.v4.widget.DrawerLayout>    </RelativeLayout> 
然后是mainui_activity.xml 就显示hello
<?xml version="1.0" encoding="utf-8"?><!--     Copyright (C) 2007 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License at            http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_vertical"    android:orientation="vertical" >  <TextView        android:layout_width="match_parent"<span style="white-space:pre"></span>android:layout_height="match_parent"        android:focusable="true"        android:gravity="center"        android:paddingBottom="2dip"        android:paddingTop="7dip"        android:text="hello"/>
</LinearLayout>

然后是左侧栏布局 leftlayout_drawer.xml 这个也随便放控件 这里用listview
<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="@android:color/transparent"    android:orientation="vertical" >           <ListView          android:id="@+id/leftlistview"          android:background="@drawable/shape_0"        android:layout_marginTop="64dip"        android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:cacheColorHint="#00000000"           android:dividerHeight="1dp"          android:fadingEdge="none"          android:divider="@drawable/shape"        android:footerDividersEnabled="false"          android:headerDividersEnabled="false"          android:listSelector="#00000000" >      </ListView>  </LinearLayout> 
接下来就是代码了 MainActivity.java<pre name="code" class="java">/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.drawertest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.support.v4.widget.DrawerLayout;import android.view.View;import android.view.Window;import android.widget.AdapterView;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.SimpleAdapter;import android.widget.SimpleCursorAdapter;import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity{@Overridepublic void onBackPressed() {// TODO Auto-generated method stubsuper.onBackPressed();}private DrawerLayout mDrawerLayout ; RelativeLayout leftLayout;  private ListView leftList;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);requestWindowFeature(Window.FEATURE_NO_TITLE);setVolumeControlStream(AudioManager.STREAM_MUSIC);
<span style="white-space:pre"></span>setContentView(R.layout.main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  leftLayout=(RelativeLayout) findViewById(R.id.left); leftList = (ListView) findViewById(R.id.leftlistview);leftList.setOnItemClickListener(ListOnItemClickListener);initSettingList();}private OnItemClickListener ListOnItemClickListener = new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {<span style="font-family: Arial, Helvetica, sans-serif;">//这里就可以实现菜单功能 </span>
//Intent intent = new Intent();switch(position){case 0:
break;case 1:break;case 2:break;case 3:break;case 4:break;case 5:break;case 6:break;}//startActivity(intent);return;}}; public void initSettingList(){List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();String[] setchoose = {"Local","Remote","Setup"};for (int i = 0; i < setchoose.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("fileName", setchoose[i]);//map.put("icon", R.drawable.turn);lists.add(map);}SimpleAdapter sa = new SimpleAdapter(this,lists, R.drawable.mylist1, new String[] {"fileName" ,"icon"},new int[] { R.id.setuptext, R.id.setupturn });leftList.setAdapter(sa);} @Overridepublic void onDestroy() {}}

                                             
0 0