android抽屜SlidingDrawer——wrap_content非全屏

來源:互聯網
上載者:User

本文地址: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>即可。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.