本文地址:http://blog.csdn.net/you_and_me12/article/details/7327030

SlidingDrawer的用法比较多,就不说明了。

如果设置了属性layout_height="wrap_content"是不起作用的,在原来的SlidingDrawer中的onMeasure()方法使得layout_height一直都是fill_parent,但是使用layout_height="固定高度"也是可以!

以下重写SlidingDrawer,实现如下效果图(右图效果):

-->

贴代码

/** * 使得SlidingDrawer在屏幕低端,而不会填满整个屏幕 * @author akai 2012-03-06 */public class WrapSlidingDrawer extends SlidingDrawer {    private boolean mVertical;    private int mTopOffset;        public WrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);        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);    }    @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();        measureChild(handle, widthMeasureSpec, heightMeasureSpec);        if (mVertical) {            int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;            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();        }        setMeasuredDimension(widthSpecSize, heightSpecSize);    }}

注释:本代码可以直接使用,不需修改,使用是把原来的<SlidingDrawer>标签修改为<packageName.WrapSlidingDrawer>即可。


更多相关文章

  1. Mac系统下Android(安卓)SDK更新以及ADT无法在线下载
  2. android 实现listView异步加载图片
  3. Android(安卓)Studio3.0之后gradle.build的修改(implementation,
  4. android调节屏幕亮度(包括只修改应用程序和修改系统)
  5. 转载:重新签名,修改系统时间,android.uid.system
  6. Android中visibility属性详解
  7. Android(安卓)Binder机制分析(4) Parcel类分析
  8. android SDK更新不了的问题
  9. android将ROM改为默认开启调试模式和未知源,默认关闭GPS

随机推荐

  1. shell变量的类型
  2. 2021上半年软考报名流程详解!赶紧了解一下
  3. 驱动下一代技术浪潮,我们更专注价值落地
  4. Microsoft Ignite 2021:Azure中管理VM的新
  5. redis中的 geospatial(地理位置)使用
  6. 长相对于经济学的学术生涯重要吗? 牛年首
  7. 天下回归, 无内生性不破, 唯此神文不破,
  8. 各地软考报考已开始,请大家及时报考
  9. 深度排序模型在淘宝直播的演进与应用
  10. 百度知识图谱技术及应用