温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用中怎么实现一个抽屉效果

发布时间:2020-12-08 16:08:59 来源:亿速云 阅读:180 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关Android应用中怎么实现一个抽屉效果,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先在layout 下设置xml布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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>

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();
    }
    public Object getItem(int position) {
      // TODO Auto-generated method stub
      return apps.get(position);
    }
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      ImageView imageView = null;
      if (convertView == null) {
        imageView = new ImageView(Chouti.this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
      } else {
        imageView = (ImageView) convertView;
      }
      ResolveInfo ri = apps.get(position);
      imageView.setImageDrawable(ri.activityInfo
          .loadIcon(getPackageManager()));
      return imageView;
    }
  }
}

关于Android应用中怎么实现一个抽屉效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI