android之抽屉布局应用

我们有时后希望在一个界面中显示另外一个布局,但是又要不需要占用太多布局空间,这个时候我们可以考虑用到抽屉布局,这个名词大家可以充分发挥能动想象,相信通过下面的介绍大家一定可以对这种方式有所了解.

好了,直接上一个简单的小项目.

1、首先我们建一个主activity

[java]
package com.jindegege.activity; 
 
import com.jindegege.service.MyAdapter; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.SlidingDrawer; 
public class SlidingdrawerActivity extends Activity { 
      
      private GridView gridview; 
      private SlidingDrawer slidingdrawer; 
      private ImageView imageview; 
      private int[] icons={R.drawable.main1,R.drawable.main2, 
                            R.drawable.main3,R.drawable.main4, 
                            R.drawable.main5,R.drawable.main6, 
                            R.drawable.main7,R.drawable.main8,R.drawable.main9}; 
      private String[] items={"华仔","发哥","雅芝","柏芝","周星星","jindegege","老毛","老毕","涵涵"}; 
          
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            gridview = (GridView)findViewById(R.id.gridview);  
            slidingdrawer = (SlidingDrawer)findViewById(R.id.sd); 
            imageview=(ImageView)findViewById(R.id.imageview); 
            MyAdapter adapter=new MyAdapter(this,items,icons);//通过构造函数实例化一个MyAdapter对象,这个MyAdapter对象必须继承BaseAdapter类  
            gridview.setAdapter(adapter); 
            slidingdrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()//打开抽屉  
            { 
              @Override 
              public void onDrawerOpened() 
              { 
                  imageview.setImageResource(R.drawable.photo);//打开抽屉事件   
              } 
            }); 
            slidingdrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() 
            { 
              @Override 
              public void onDrawerClosed() 
              { 
                  imageview.setImageResource(R.drawable.ic_launcher);//关闭抽屉事件  
              } 
            }); 
        } 
    } 
package com.jindegege.activity;

import com.jindegege.service.MyAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
public class SlidingdrawerActivity extends Activity {
 
   private GridView gridview;
   private SlidingDrawer slidingdrawer;
   private ImageView imageview;
   private int[] icons={R.drawable.main1,R.drawable.main2,
                         R.drawable.main3,R.drawable.main4,
                         R.drawable.main5,R.drawable.main6,
                         R.drawable.main7,R.drawable.main8,R.drawable.main9};
   private String[] items={"华仔","发哥","雅芝","柏芝","周星星","jindegege","老毛","老毕","涵涵"};
     
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         gridview = (GridView)findViewById(R.id.gridview);
         slidingdrawer = (SlidingDrawer)findViewById(R.id.sd);
         imageview=(ImageView)findViewById(R.id.imageview);
         MyAdapter adapter=new MyAdapter(this,items,icons);//通过构造函数实例化一个MyAdapter对象,这个MyAdapter对象必须继承BaseAdapter类
         gridview.setAdapter(adapter);
         slidingdrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()//打开抽屉
         {
           @Override
           public void onDrawerOpened()
           {
            imageview.setImageResource(R.drawable.photo);//打开抽屉事件
           }
         });
         slidingdrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()
         {
           @Override
           public void onDrawerClosed()
           {
            imageview.setImageResource(R.drawable.ic_launcher);//关闭抽屉事件
           }
         });
     }
 }
 
2、新建这个主activity要加载的布局文件

[html]
<?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" 

  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:textSize="20sp" 
  /> 
  <SlidingDrawer  
    android:id="@+id/sd"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:handle="@+id/imageview"  
    android:content="@+id/gridview"  
    android:orientation="vertical" 
  > 
 
      <ImageView 
        android:id="@id/imageview" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:src="@drawable/ic_launcher" 
      /> 
 
      <GridView  
      android:id="@id/gridview"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:numColumns="3" 
      android:background="#EE82EE"  
      android:gravity="center" 
    />   
        
  </SlidingDrawer> 
</RelativeLayout> 
<?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"
>
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:textSize="20sp"
  />
  <SlidingDrawer
    android:id="@+id/sd"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:handle="@+id/imageview"
    android:content="@+id/gridview"
    android:orientation="vertical"
  >

      <ImageView
        android:id="@id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
      />

      <GridView
      android:id="@id/gridview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:numColumns="3"
      android:background="#EE82EE"
      android:gravity="center"
    /> 
      
  </SlidingDrawer>
</RelativeLayout>

第三步:在主activity中,我们要通过构造函数实例化一个MyAdapter对象,这个MyAdapter对象必须继承BaseAdapter类,

以用来自定义一个适配器,先新建一个xml文件item.xml,这只是一个简单的样式。

[html]
<?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="40px" 
    android:layout_gravity="center" 
  /> 
  <TextView  
    android:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textColor="#000000" 
  /> 
</LinearLayout> 
<?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="40px"
    android:layout_gravity="center"
  />
  <TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#000000"
  />
</LinearLayout>
第四步:这个时候就是设计我们自定义的类了。

[java]
package com.jindegege.service; 
 
import com.jindegege.activity.R; 
 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
public class MyAdapter extends BaseAdapter  
{  
  private Context context; 
  private String[] items; 
  private int[] icons; 
 
  public MyAdapter(Context context,String[] items,int[] icons) //构造器  
  { 
    this.context=context; 
    this.items=items; 
    this.icons=icons; 
  } 
 
  @Override 
  public int getCount() 
  { 
    return items.length; 
  } 
 
  @Override 
  public Object getItem(int arg0) 
  { 
    return items[arg0]; 
  } 
 
  @Override 
  public long getItemId(int position) 
  { 
    return position; 
  } 
 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) 
  { 
    LayoutInflater factory = LayoutInflater.from(context); 
    View v = (View) factory.inflate(R.layout.item, null);//绑定自定义的layout  
    ImageView iv = (ImageView) v.findViewById(R.id.icon); 
    TextView tv = (TextView) v.findViewById(R.id.text); 
    iv.setImageResource(icons[position]); 
    tv.setText(items[position]); 
    return v; 
  } 
 
  

package com.jindegege.service;

import com.jindegege.activity.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter
{
  private Context context;
  private String[] items;
  private int[] icons;

  public MyAdapter(Context context,String[] items,int[] icons) //构造器
  {
    this.context=context;
    this.items=items;
    this.icons=icons;
  }

  @Override
  public int getCount()
  {
    return items.length;
  }

  @Override
  public Object getItem(int arg0)
  {
    return items[arg0];
  }

  @Override
  public long getItemId(int position)
  {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    LayoutInflater factory = LayoutInflater.from(context);
    View v = (View) factory.inflate(R.layout.item, null);//绑定自定义的layout
    ImageView iv = (ImageView) v.findViewById(R.id.icon);
    TextView tv = (TextView) v.findViewById(R.id.text);
    iv.setImageResource(icons[position]);
    tv.setText(items[position]);
    return v;
  }

 
}

 

好的,效果图就不给大家贴出了,大家可以自己下载该小项目,看看效果,自己也可以修改实现更好的效果。

源代码下载地址:http://www.2cto.com/uploadfile/2012/0227/20120227114119342.rar


摘自 jindegegesun的专栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值