自定义控件:抽屉SlidingDrawer——wrap_content非全屏

自定义控件:抽屉SlidingDrawer——wrap_content非全屏

android:allowSingleTap   指示抽屉是否可以打开/通过手柄上的一个水龙头关闭。

android:animateOnClick  表示所述抽屉是否应该打开/与当用户点击手柄动画关闭。      

android:bottomOffset     额外偏移的把手在SlidingDrawer的底部。
android:content   标识符表示抽屉的内容孩子。

Identifier for the child that represents the drawer's content. 
android:handle     标识符表示抽屉的把手的孩子。

Identifier for the child that represents the drawer's handle.
android:orientation

Orientation of the SlidingDrawer.  取向SlidingDrawer的。
android:topOffset

Extra offset for the handle at the top of the SlidingDrawer.

额外偏移的把手在SlidingDrawer的顶部。

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer; /**
* 使得SlidingDrawer在屏幕低端,而不会填满整个屏幕
*/
public class WrapSlidingDrawer extends SlidingDrawer { private boolean mVertical;
private int mTopOffset; // 从指定的XML定义的属性集创建一个新的WrapSlidingDrawer。
public WrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
/**
* namespace 属性的命名空间来获取。
* attribute 属性检索。
* defaultValue 如果未找到该属性返回什么。
*
* ORIENTATION_VERTICAL:定向垂直。
*/
int orientation = attrs.getAttributeIntValue("android", "orientation",
ORIENTATION_VERTICAL);
// "topOffset" 额外偏移的把手在SlidingDrawer的顶部。
mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
} public WrapSlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
int orientation = attrs.getAttributeIntValue("android", "orientation",
ORIENTATION_VERTICAL);
mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
} /**
* 测量视图和其内容,以确定所测量的宽度和所测量的高度。
* widthMeasureSpec 宽度测量规格。
* heightMeasureSpec 高度测量规格。
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
// 返回抽屉的手柄。
final View handle = getHandle();
// 返回抽屉的内容。
final View content = getContent();
/**
* Ask one of the children of this view to measure itself, taking into
* account both the MeasureSpec requirements for this view and its padding.
* The heavy lifting is done in getChildMeasureSpec.
*
* @param child The child to measure
* @param parentWidthMeasureSpec The width requirements for this view
* @param parentHeightMeasureSpec The height requirements for this view
*/
measureChild(handle, widthMeasureSpec, heightMeasureSpec); if (mVertical) {
// 测量高度 - 抽屉手柄高度 - 额外偏移的把手顶部
int height = heightSpecSize - handle.getMeasuredHeight()
- mTopOffset;
// 内容尺寸
// public static int makeMeasureSpec (int size, int mode)
// Creates a measure specification based on the supplied size and mode.
// size the size of the measure specification
// mode the mode of the measure specification
content.measure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(height, heightSpecMode));
//
heightSpecSize = handle.getMeasuredHeight() + mTopOffset
+ content.getMeasuredHeight();
//
widthSpecSize = content.getMeasuredWidth();
//
if (handle.getMeasuredWidth() > widthSpecSize)
widthSpecSize = handle.getMeasuredWidth();
} else {
int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
getContent().measure(
MeasureSpec.makeMeasureSpec(width, widthSpecMode),
heightMeasureSpec);
widthSpecSize = handle.getMeasuredWidth() + mTopOffset
+ content.getMeasuredWidth();
heightSpecSize = content.getMeasuredHeight();
if (handle.getMeasuredHeight() > heightSpecSize)
heightSpecSize = handle.getMeasuredHeight();
}
// 此方法必须由onMeasure(int,int)被调用储存的测量宽度和高度测量。
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
 <?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:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center"
android:text="AAAAAAAAAAAAAA"
android:textSize="10pt" /> <com.cn.daming.WrapSlidingDrawer
android:id="@+id/sliding_drawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:content="@+id/mycontent"
android:handle="@+id/layout1"
android:layout_alignParentBottom="true"
android:orientation="vertical" > <LinearLayout
android:id="@id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center" > <ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/up1" />
</LinearLayout> <GridView
android:id="@id/mycontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff000000"
android:gravity="center"
android:numColumns="3"
android:paddingTop="20dip" />
</com.cn.daming.WrapSlidingDrawer> </RelativeLayout>

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView; public class SlidingDrawerMainActivity extends Activity { private GridView gridView;
private SlidingDrawer slidingDrawer;
private ImageView imageView;
private TextView textview;
private int[] icons = { R.drawable.title1, R.drawable.title2,
R.drawable.title3, R.drawable.title4, R.drawable.title5,
R.drawable.title6 }; private String[] items = { "Phone", "Message", "AddImage", "Music",
"Telephone", "SMS" }; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.mycontent);
slidingDrawer = (SlidingDrawer) findViewById(R.id.sliding_drawer);
imageView = (ImageView) findViewById(R.id.my_image);
textview = (TextView) findViewById(R.id.text_view);
MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);
gridView.setAdapter(adapter); slidingDrawer
.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { public void onDrawerOpened() {
textview.setVisibility(View.GONE);
imageView.setImageResource(R.drawable.down1);
}
});
slidingDrawer
.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { public void onDrawerClosed() {
textview.setVisibility(View.VISIBLE);
imageView.setImageResource(R.drawable.up1);
}
});
} @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}

自定义控件:抽屉SlidingDrawer——wrap_content非全屏

完整代码下载:http://pan.baidu.com/s/1sjsdqTv

上一篇:洛谷P1605:迷宫(DFS)


下一篇:洛谷P1605 迷宫