这里是我的空间,是我用来记录点滴的沃土...

android 进阶之android中隐藏的layout (抽屉)

main.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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="20sp" />

    <SlidingDrawer
        android:id="@+id/sd"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:content="@+id/myContent"
        android:handle="@+id/iv"
        android:orientation="vertical" >

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

        <GridView
            android:id="@id/myContent"
            android:layout_width="wrap_content"
            android:layout_height="344dp"
            android:gravity="center"
            android:numColumns="3" />
    </SlidingDrawer>

</RelativeLayout>

GridView的子控件布局 gv.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="40px"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#ffffffff" />

</LinearLayout>

java代码:

package com.wangsx.slid;

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 SlideDrawerDemoActivity extends Activity {
    private GridView gv;
    private SlidingDrawer sd;
    private ImageView iv;
    private int[] icons = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
    private String[] items = { "浏览器", "图片", "相机", "时钟", "音乐", "市场", "拨号", "信息", "地图" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv = (GridView) findViewById(R.id.myContent);
        sd = (SlidingDrawer) findViewById(R.id.sd);
        iv = (ImageView) findViewById(R.id.iv);
        MyAdapter adapter = new MyAdapter(this, items, icons);// 自定义MyAdapter来实现图标加item的显示效果
        gv.setAdapter(adapter);
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 开抽屉
        {
            @Override
            public void onDrawerOpened() {
                iv.setImageResource(R.drawable.close);// 响应开抽屉事件 ,把图片设为向下的
            }
        });
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                iv.setImageResource(R.drawable.open);// 响应关抽屉事件
            }
        });
    }

    class MyAdapter extends BaseAdapter {
        private Context _ct;
        private String[] _items;
        private int[] _icons;

        public MyAdapter(Context ct, String[] items, int[] icons) // 构造器
        {
            _ct = ct;
            _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(_ct);
            View v = (View) factory.inflate(R.layout.gv, null);// 绑定自定义的layout
            ImageView iv = (ImageView) v.findViewById(R.id.icon);
            TextView tv = (TextView) v.findViewById(R.id.text);
            iv.setImageResource(_icons[position]);
            tv.setText(_items[position]);
            return v;
        }
    }

}

效果如图

 

posted @ 2012-06-28 17:00  wsx2miao  Views(2154)  Comments(0Edit  收藏  举报
这里是我的空间,是我用来记录点滴的沃土...