可能大家会遇到一个应用图标过多不好排版也不好处理的问题吧?小马学习了下一个应用图标过多时的一个简单的处理方式,就是用安卓提供的SlidingDrawer来完成漂亮的排版与功能实现,废话不多说,先上效果图再看具体代码实现:
一: 小抽屉未展开时:
![]()
二:小抽屉展开时:
![]()
三:小抽屉中功能图标过多时(这个好神奇,竟然能自己扩充,激动呀)
![]()
四:如果要实现抽屉中不同图标的不同功能时,更简单,直接给GridView的项加事件实现就可以啦,吼吼
下面我们来看下代码实现:
- package com.xiaoma.www;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.SlidingDrawer;
- import android.widget.Toast;
- import android.widget.SlidingDrawer.OnDrawerCloseListener;
- import android.widget.SlidingDrawer.OnDrawerOpenListener;
-
-
-
-
-
-
-
- public class SlidingDrawerDemoActivityActivity extends Activity {
-
-
- private SlidingDrawer sDrawer ;
- private GridView gvGridView ;
-
- private ImageView myImage1;
-
-
-
-
-
-
-
-
-
-
-
- private int icons[] = {
- R.drawable.angry_birds,R.drawable.browser,R.drawable.dropbox,
- R.drawable.googleearth,R.drawable.lastfm,R.drawable.xiaoma,
- R.drawable.xbmc,R.drawable.youtube,R.drawable.notes,
- R.drawable.messages_dock,R.drawable.contacts,
- R.drawable.facebook,R.drawable.wapedia
- };
-
- private String items[] = {
- "愤怒的小鸟","浏览器","dropbox","谷歌地球","AS","小马果的驴子","嘛东西",
- "YouTuBe","记事本","消息提示","通讯薄","面谱","WAP"
- };
-
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
-
-
-
- private void init() {
-
- sDrawer = (SlidingDrawer)findViewById(R.id.mySliding);
-
- myImage1 = (ImageView)findViewById(R.id.myImage1);
-
- gvGridView = (GridView)findViewById(R.id.gridView);
-
-
- GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), items, icons);
- gvGridView.setAdapter(adapter);
- gvGridView.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
-
-
-
- Toast.makeText(getApplicationContext(),
- "单击了第"+position+"项", Toast.LENGTH_SHORT).show();
- }
- });
-
-
-
-
-
-
-
- sDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
-
- @Override
- public void onDrawerOpened() {
- myImage1.setImageResource(R.drawable.close);
- }
- });
-
-
- sDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
-
- @Override
- public void onDrawerClosed() {
- myImage1.setImageResource(R.drawable.open);
- }
- });
- }
- }
怎么样?简单吧?吼吼,再来看下我们的抽屉主布局,里面有比较重要的两点,大家仔细看下注释:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/background"
- android:orientation="vertical" >
- <!-- android:handle指定我们点击抽屉时的小图标,这个必须指定,否则没办法点,
- 主要是去哪点出来 ? 所以这个属性必须加
- android:content指定我们的抽屉里面加载的布局
- -->
- <SlidingDrawer
- android:id="@+id/mySliding"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:handle="@+id/layout1"
- android:content="@+id/gridView"
- android:orientation="horizontal"
- >
- <LinearLayout
- android:id="@+id/layout1"
- android:layout_width="35px"
- android:layout_height="fill_parent"
- android:gravity="center_vertical"
- >
- <ImageView
- android:id="@+id/myImage1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/open"
- />
- </LinearLayout>
- <GridView
- android:id="@+id/gridView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numColumns="3"
- android:gravity="center"
- />
- </SlidingDrawer>
- </RelativeLayout>
下面再来看下我们对网格布局填充的自定义布局,很简单的,对吧,嘿嘿
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageView
- android:id="@+id/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- />
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="20sp"
- android:gravity="center"
- android:textColor="@drawable/black"
- />
- </LinearLayout>
最后,老样子,如果小马代码写得太乱,希望看文章的你我多指正,有错必改的,吼吼,谢谢,希望多提意见给小马,这才是对小马最大的帮助,小DEMO源码小马放附件里面了,有用到的朋友可下载改改后,实现自己想要的完美功能吧,加油加油,谢谢