[置顶] 实现Android 抽屉效果

    在Android开发过程中,我们喜欢使用特效,比如抽屉效果,这样可以给人很好的体验。点击一个按钮,就像拉抽屉一样展开界面,这样的效果正是我在这里所要说明的。比如在AVD或真机上,我们都有看过这种效果。比较常用的应用是LAUNCH应用。在这个应用中我们实现了拉抽屉呈现所有的程序,在这里我参考一些别人写的博客试例讲这种实现细节。

 

创建一个工程。在这里我命名为LauncherDemo.在这个例子中我在main.xml文件中写入如下,贴上代码:

 

<?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" > <SlidingDrawer android:id="@+id/slidingdrawer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:handle="@+id/handle" android:content="@+id/content"> <Button android:id="@+id/handle" android:layout_width="88dip" android:layout_height="44dip" android:background="@drawable/handle" /> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00ff00"> <GridView android:id="@+id/allapps" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </SlidingDrawer> </LinearLayout>

 

 

看到这个SlidingDrawer控件。就像使用Button,TextView一样简单使用。

 

然后在LauncherActivity.java代码如下:

 

public class LauncherActivity extends Activity implements OnItemClickListener{ private GridView mGridView; private Context mContext; private PackageManager mPackageManager; private List<ResolveInfo> mAllApps; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } public void setupViews(){ mContext = LauncherActivity.this; mPackageManager = getPackageManager(); //包管理器 mGridView = (GridView)findViewById(R.id.allapps); bindAllApps(); mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps)); mGridView.setNumColumns(4); mGridView.setOnItemClickListener(this); } public void bindAllApps(){ //这里是关键哦,我们平时写的应用总有一个activity申明成这两个属性 //也就是应用的入口 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); //符合上面条件的全部查出来,并且排序 mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0); Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager)); } //gridview点击事件,点击进入相关应用 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub ResolveInfo res = mAllApps.get(position); //该应用的包名和主Activity String pkg = res.activityInfo.packageName; String cls = res.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent i = new Intent(); i.setComponent(componet); startActivity(i); } //不明白BaseAdapter的用法 我高手进阶里有 private class GridItemAdapter extends BaseAdapter{ private Context context; private List<ResolveInfo> resInfo; //构造函数 public GridItemAdapter(Context c,List<ResolveInfo> res){ context = c; resInfo = res; } @Override public int getCount() { // TODO Auto-generated method stub return resInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //不明白LayoutInflater的我android高手进阶里有 convertView = LayoutInflater.from(context).inflate(R.layout.application_layout, null); ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon); TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title); ResolveInfo res = resInfo.get(position); app_icon.setImageDrawable(res.loadIcon(mPackageManager)); app_tilte.setText(res.loadLabel(mPackageManager).toString()); return convertView; } } }

 

 

实现效果如下所示:

 

[置顶] 实现Android 抽屉效果_第1张图片

 

 

 

 

 

 

你可能感兴趣的:(android,layout,null,Class,button,encoding)