android 多个activity侧滑栏的实现
来源:互联网 发布:缺陷检测算法 编辑:程序博客网 时间:2024/06/05 09:20
最近项目需要做侧滑栏来实现菜单,时间比较紧,所以就从网上找了很多代码看,刚开始想用slidingMenu 这个去做,虽然功能比较多但是发现用起来很麻烦,而我是极其不喜欢麻烦的人,于是找了官方API,发现了 DrawerLayout这个东西,用起来方便,也适合在多个activity中用。废话不说了,下面讲解代码:首先是main.xml
然后是左侧栏布局 leftlayout_drawer.xml 这个也随便放控件 这里用listview
<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
- android 多个activity侧滑栏的实现
- Android如何实现多个Activity的界面皮肤更换
- Android 多个Activity选项卡实现
- 多个Activity轮训切换的实现
- Android退出多个activity和单个activity的方法
- Android中包含多个Activity的应用退出按钮实现
- Android中包含多个Activity的应用退出按钮实现
- Android中包含多个Activity的应用退出按钮实现
- Android junit实现多个Activity跳转测试
- android中一个activity实现多个xml页面互换。
- Android junit实现多个Activity跳转测试
- Android中实现多个Activity共享数据
- 重写Activity实现多个Activity引用同一个Activity的按钮布局和响应
- Android新增Activity,并实现多Activity之间的切换
- Android 打开多个Activity时,同时结束多个Activity的方法
- Android 删除多个activity的类,进入多个activity
- Android 关闭多个Activity
- Android关闭多个activity
- 用js控制flash来实现全屏效果
- 对自己工作和生活有帮助的几则故事
- 不小心清空的回收站文件怎么找回
- python实现递归算法
- 关于lib的调用与dll编译后为什么没有lib文件出现
- android 多个activity侧滑栏的实现
- 交叉编译ffmpeg并生成ffplay
- ubuntu svn checkout 报错
- 7-3. 数素数 (20)
- SQL注入:无法监测则无从防护
- 内存不足:杀死进程还是牺牲子进程
- 超级大 正整数 乘法
- C语言中const
- Android 启动系统相机功能 并返回所拍照片