這裏來講一個已經被廢棄的SlidingDrawer.. 他能夠實現上拉,下拉的菜單。 可是有個問題就是上拉之後,是全屏顯示的。android
首先 寫一個佈局:app
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eeeeee" tools:context="com.wingsofts.slidingdrawer.MainActivity"> <SlidingDrawer android:content="@+id/content" android:handle="@+id/handle" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:id="@+id/content" android:background="#ac2d9a" android:layout_width="match_parent" android:layout_height="300dp"/> </SlidingDrawer> </RelativeLayout>
而後運行效果是這樣的:
ide
誒。。怎麼不對!!! 我菜單寫的高度是300啊。。
而後試試wrap_content,仍是不行。。
那麼該怎麼辦呢。。。 stackoverflow上面有大神給出了答案。
即:重寫SlidingDrawer的onMeasure方法,讓他支持wrap_content;佈局
新建類以下:code
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.SlidingDrawer; public class WrappingSlidingDrawer extends SlidingDrawer { public WrappingSlidingDrawer(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 WrappingSlidingDrawer(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); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions"); } 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); } private boolean mVertical; private int mTopOffset; }
這裏,再把Slidingdrawer外面包裹一個LinearLayout,再把其高度設置爲wrap_content便可,注意android:gravity=」bottom」。xml
修改後的佈局文件以下:blog
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eeeeee" tools:context="com.wingsofts.slidingdrawer.MainActivity"> <LinearLayout android:gravity="bottom" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.wingsofts.slidingdrawer.WrappingSlidingDrawer android:content="@+id/content" android:handle="@+id/handle" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:background="#ac2d9a" android:layout_width="match_parent" android:layout_height="300dp"/> </LinearLayout> </com.wingsofts.slidingdrawer.WrappingSlidingDrawer> </LinearLayout> </RelativeLayout>
大功告成!! 看效果:
圖片
stackoverflow連接:http://stackoverflow.com/questions/3654492/android-can-height-of-slidingdrawer-be-set-with-wrap-content/4265553#4265553utf-8