这里来说一个已经被废弃的SlidingDrawer.. 他可以实现上拉,下拉的菜单。 但是有个问题就是上拉以后,是全屏显示的。

首先 写一个布局:

<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>

然后运行效果是这样的:

诶。。怎么不对!!! 我菜单写的高度是300啊。。

然后试试wrap_content,还是不行。。

那么该怎么办呢。。。 stackoverflow上面有大神给出了答案。

即:重写SlidingDrawer的onMeasure方法,让他支持wrap_content;

新建类如下:

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 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#4265553

Android 上滑上拉菜单SlidingDrawer 不全屏显示的方法的更多相关文章

  1. Swiper 判断上滑下拉操作

    onTouchMove: function(swiper){ //手动滑动中触发//判断上滑下拉var i = mySwiper.translate;setTimeout(function() {va ...

  2. ios position:fixed 上滑下拉抖动

    ios position:fixed 上滑下拉抖动 最近呢遇到一个ios的兼容问题,界面是需要一个头底部的固定的效果,用的position:fixed定位布局,写完测试发现安卓手机正常的,按时ios上 ...

  3. Android:有关下拉菜单导航的学习(供自己参考)

    Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...

  4. 不全屏显示、手柄不居中的SlidingDrawer

    SlidingDrawer是一个滑动式抽屉,通过点击或拖拽手柄(handle)来显示或隐藏内容(content). 看了很多关于SlidingDrawer的例子,但基本都是全屏显示,并且手柄居中的.我 ...

  5. js实现页面的上滑下拉功能

    这两天做项目,用到了上滑和下拉的功能,主要是通过监听touchmove,touchstart,touchend三个事件去判断页面上滑状态还是下拉状态. 同时加一个知识点:有时在监听时会报错,这个错是这 ...

  6. phpcms v9 下拉菜单 二级 三级子栏目调用方法

    很多网站的导航栏可以实现下拉二级菜单,三级菜单等效果,今天我们就来分享phpcms v9 支持下拉菜单的方法,可以支持无限子栏目调用,具体写法如下: <ul> {pc:content ac ...

  7. Android 全屏显示的方法(不包含状态栏)

    我们都知道在Android中某些功能的实现往往有两种方法:一种是在xml文件中设置相应属性,另一种是用代码实现.同样Android实现全屏显示也可以通过这两种方法实现: 1.在AndroidManif ...

  8. Android实现全屏显示的方法

    一种是在xml文件中设置相应属性,另一种是用代码实现. 1.在AndroidManifest.xml的配置文件里面的<activity>标签添加属性: android:theme=&quo ...

  9. Delphi 工具条按钮上的下拉菜单

    制作步骤: 1.添加一个 TImageList: ImageList1, 然后载入些图标; 2.添加两个 TPopupMenu: PopupMenu1.PopupMenu2, 并分别添加些菜单项; 3 ...

随机推荐

  1. Docker 网络

    Docker 的网络实现其实就是利用了 Linux 上的网络名字空间和虚拟网络设备(特别是 veth pair).建议先熟悉了解这两部分的基本概念再阅读本章. 基本原理 首先,要实现网络通信,机器需要 ...

  2. android JNI的.so库调用

    在一篇博客中看到一篇文章,感觉描述的还可以: 在前面的博客中介绍的都是使用java开发Android应用,这篇博客将介绍java通过使用jni调用c语言做开发 为了更加形象的介绍jni,先观察下面的图 ...

  3. Mongo Index

    摘要 mongo 的索引非常强大,和关系型数据库索引没什么区别.这里主要介绍本人在mongo索引上的犯的错. 索引种类 1.单字段索引 2.复合索引 多个字段索引 如{name:1,address:1 ...

  4. ejabberd开发和部署

    ejabberd开发和部署 (金庆的专栏 2016.10) 搭建了自己的ejabberd集群,然后少量更改源码,实现定制的XMPP服务器. 从github fork ejabberd 库,定为 mas ...

  5. Swift中if与switch语句使用一例

    在Swift中相同的条件处理有if和switch两个语句,我们如何取舍呢? 一种情况下我们只在乎x是否在一个范围内,但并不关心x是否穷尽!换句话说不关心在满足范围条件的其他情况下,此时我们可以考虑用i ...

  6. 剑指Offer——“你最大的缺点是什么”回答技巧及范例

    剑指Offer--"你最大的缺点是什么"回答技巧及范例   问题分析:认识自己的缺点是一个巨大的优点, 当HR问到你缺点的时候, 你的机会来了, 请快展示你的自知之明吧!你想把优点 ...

  7. SKSpriteNode对象初始化在iPhone 6 plus中显示不正确的分析及解决

    一个SpriteKit项目在其他设备上运行都无问题(无论是真机或是模拟器),但是在iPhone6 Plus上会出现精灵对象纹理被过度放大的现象: 从上图中大家可以看到无论是主角或是道具球都过大了. 看 ...

  8. SpriteKit游戏Delve随机生成地牢地图一个Bug的修复

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Delve是一个很有意思的地牢探险类型的游戏,其中每一关的地图 ...

  9. Scikit-learn:scikit-learn快速教程及实例

    http://blog.csdn.net/pipisorry/article/details/52251305 :7] y = dataset[:,8] 我们将在下面所有的例子里使用这个数据组,换言之 ...

  10. tomcat中http与https协议socket工厂