发布于 2017-10-09 12:06:54 | 181 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现抽屉效果的方法,结合具体实例形式分析了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>

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;
    }
  }
}

loadApps方法是得到主界面上的图片和文字。

然后设置的自定义adapter中去。

看下运行后效果:

向上滑动imageview按钮后:

为了体现更好的效果,可以用两张滑动图片,一张朝上的,一张朝下的。根据监听器做相应的切换。

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务