用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

Android 隐藏式抽屉 SlidingDrawer

2012-10-29 作者: 程序猿style举报

[android]代码库

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class EX04_27 extends Activity {
	private GridView gv;
	private SlidingDrawer sd;
	private ImageView im;
	private int[] icons = { R.drawable.alarm, R.drawable.calendar,
			R.drawable.camera, R.drawable.clock, R.drawable.music,
			R.drawable.tv };
	private String[] items = { "Alarm", "Calendar", "Camera", "Clock", "Music",
			"TV" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/* 加载main.xml Layout */
		setContentView(R.layout.main);
		/* 初始化对象 */
		gv = (GridView) findViewById(R.id.myContent1);
		sd = (SlidingDrawer) findViewById(R.id.drawer1);
		im = (ImageView) findViewById(R.id.myImage1);

		/* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */
		MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);
		gv.setAdapter(adapter);

		/* 设定SlidingDrawer被打开的事件处理 */
		sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
			@Override
			public void onDrawerOpened() {
				im.setImageResource(R.drawable.close);
			}
		});
		/* 设置SlidingDrawer被关闭的事件处理 */
		sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
			@Override
			public void onDrawerClosed() {
				im.setImageResource(R.drawable.open);
			}
		});
	}

}

/* 定义Adapter,继承BaseAdapter */
class MyGridViewAdapter extends BaseAdapter {
	private Context _con;
	private String[] _items;
	private int[] _icons;

	/* 构造符 */
	public MyGridViewAdapter(Context con, String[] items, int[] icons) {
		_con = con;
		_items = items;
		_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(_con);
		/* 使用grid.xml为每几个item的Layout */
		View v = (View) factory.inflate(R.layout.grid, null);
		/* 取得View */
		ImageView iv = (ImageView) v.findViewById(R.id.icon);
		TextView tv = (TextView) v.findViewById(R.id.text);
		/* 设定显示的Image与文? */
		iv.setImageResource(_icons[position]);
		tv.setText(_items[position]);
		return v;
	}
}




<?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="16sp" />

    <SlidingDrawer
        android:id="@+id/drawer1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/myContent1"
        android:handle="@+id/layout1"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@id/layout1"
            android:layout_width="35px"
            android:layout_height="fill_parent"
            android:background="@drawable/black"
            android:gravity="center" >

            <ImageView
                android:id="@+id/myImage1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/open" />
        </LinearLayout>

        <GridView
            android:id="@id/myContent1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black"
            android:gravity="center"
            android:numColumns="2" />
    </SlidingDrawer>

</RelativeLayout>


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...