这里来说一个已经被废弃的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. Go 语言教程

    Go 语言教程 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发, ...

  2. Bootstrap3 代码-代码块

    多行代码可以使用 <pre> 标签.为了正确的展示代码,注意将尖括号做转义处理. <p>Sample text here...</p> <pre>< ...

  3. Objective-C构造方法

    Objective-C构造方法 构造方法:用来初始化的方法 创建对象的原理 之前我们创建对象的方式一直是使用[Xxx new] 但是使用 new 创建的对象,都是给我们默认做了初始化的. 有的时候,我 ...

  4. android galley实现画廊效果

    青春流水指间. 每段路,都有即将要来的旅程 每颗心,都有值得期待的成分 Android之ImageSwitcher,Gallery用法 今天在做一个软件界面时用到了ImageSwitcher和Gall ...

  5. Android状态栏透明(沉浸式效果)

    Android状态栏透明(沉浸式效果) 默认效果 沉浸式效果 方式一 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q487880 ...

  6. 详解EBS接口开发之供应商导入补充-供应商地点增加实例

    DECLARE --v_org_id number; v_vendor_interface_id NUMBER; v_vendor_site_interface_id NUMBER; --接口表的id ...

  7. Android上下文菜单ContentView详解

    ContentView介绍 上下文菜单继承了android.view.Menu,因此我们可以像操作Options Menu那样给上下文菜单增加菜单项.上下文菜单与Options Menu最大的不同在于 ...

  8. Cocos2D两个方法的重构一例

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在RPG游戏项目的GameSpace类中原来有一个方法: -( ...

  9. Java进阶(四十)Java类、变量、方法修饰符讲解

    Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...

  10. ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人

    ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人 先附上资料网址: 1.  https://developer.leapmoti ...