当前位置:操作系统 > 安卓/Android >>

android抽屉实现

今天在手机上实现了抽屉效果,其实很简单,但是效果却很酷。
 
           首先在layout 下设置xml布局文件
      
 
 
<?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" > 
 
    <SlidingDrawer 
        android:id="@+id/sliding" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:content="@+id/allApps" 
        android:handle="@+id/imageViewIcon" 
        android:orientation="vertical" > 
 
        <GridView 
            android:id="@+id/allApps" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/bk" 
            android:columnWidth="60dp" 
            android:gravity="center" 
            android:horizontalSpacing="10dp" 
            android:numColumns="auto_fit" 
            android:padding="10dp" 
            android:stretchMode="columnWidth" 
            android:verticalSpacing="10dp" /> 
 
        <ImageView 
            android:id="@+id/imageViewIcon" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/touch_handler" /> 
    </SlidingDrawer> 
 
</RelativeLayout> www.zzzyk.com
 
        SlidingDrawer就是重要的抽屉控件 ,handle是抽屉的拖动按钮,content是抽屉中的内容。
      然后建立chouti的activity类:
 
 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.ResolveInfo; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.SlidingDrawer; 
 
public class Chouti extends Activity { 
    private GridView gv; 
    private SlidingDrawer sd; 
    private ImageView iv; 
    private List<ResolveInfo> apps; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.slidingdrawer); 
        loadApps(); 
        gv = (GridView) findViewById(R.id.allApps); 
        sd = (SlidingDrawer) findViewById(R.id.sliding); 
        iv = (ImageView) findViewById(R.id.imageViewIcon); 
        gv.setAdapter(new GridAdapter()); 
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 开抽屉 
        { 
            @Override 
            public void onDrawerOpened() { 
                iv.setImageResource(R.drawable.touch_handler);// 响应开抽屉事件 
                                                                // ,把图片设为向下的 
            } 
        }); 
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { 
            @Override 
            public void onDrawerClosed() { 
                iv.setImageResource(R.drawable.touch_handler);// 响应关抽屉事件 
            } 
        }); 
    } 
 
    private void loadApps() { 
        Intent intent = new Intent(Intent.ACTION_MAIN, null); 
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
 
        apps = getPackageManager().queryIntentActivities(intent, 0); 
    } 
 
    public class GridAdapter extends BaseAdapter { 
        public GridAdapter() { 
 
        } 
 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return apps.size(); 
        } 
 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,