Android 上滑上拉菜单SlidingDrawer 不全屏显示的方法
这里来说一个已经被废弃的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 不全屏显示的方法的更多相关文章
- Swiper 判断上滑下拉操作
onTouchMove: function(swiper){ //手动滑动中触发//判断上滑下拉var i = mySwiper.translate;setTimeout(function() {va ...
- ios position:fixed 上滑下拉抖动
ios position:fixed 上滑下拉抖动 最近呢遇到一个ios的兼容问题,界面是需要一个头底部的固定的效果,用的position:fixed定位布局,写完测试发现安卓手机正常的,按时ios上 ...
- Android:有关下拉菜单导航的学习(供自己参考)
Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...
- 不全屏显示、手柄不居中的SlidingDrawer
SlidingDrawer是一个滑动式抽屉,通过点击或拖拽手柄(handle)来显示或隐藏内容(content). 看了很多关于SlidingDrawer的例子,但基本都是全屏显示,并且手柄居中的.我 ...
- js实现页面的上滑下拉功能
这两天做项目,用到了上滑和下拉的功能,主要是通过监听touchmove,touchstart,touchend三个事件去判断页面上滑状态还是下拉状态. 同时加一个知识点:有时在监听时会报错,这个错是这 ...
- phpcms v9 下拉菜单 二级 三级子栏目调用方法
很多网站的导航栏可以实现下拉二级菜单,三级菜单等效果,今天我们就来分享phpcms v9 支持下拉菜单的方法,可以支持无限子栏目调用,具体写法如下: <ul> {pc:content ac ...
- Android 全屏显示的方法(不包含状态栏)
我们都知道在Android中某些功能的实现往往有两种方法:一种是在xml文件中设置相应属性,另一种是用代码实现.同样Android实现全屏显示也可以通过这两种方法实现: 1.在AndroidManif ...
- Android实现全屏显示的方法
一种是在xml文件中设置相应属性,另一种是用代码实现. 1.在AndroidManifest.xml的配置文件里面的<activity>标签添加属性: android:theme=&quo ...
- Delphi 工具条按钮上的下拉菜单
制作步骤: 1.添加一个 TImageList: ImageList1, 然后载入些图标; 2.添加两个 TPopupMenu: PopupMenu1.PopupMenu2, 并分别添加些菜单项; 3 ...
随机推荐
- Docker实例:创建一个点到点连接
默认情况下,Docker 会将所有容器连接到由 docker0 提供的虚拟子网中. 用户有时候需要两个容器之间可以直连通信,而不用通过主机网桥进行桥接. 解决办法很简单:创建一对 peer 接口,分别 ...
- Android系统对话框
Android系统对话框 效果图 2个按钮的对话框 3个按钮的对话框 自定义View的对话框 单选对话框 多选对话框 列表框 Code XML <?xml version="1.0&q ...
- Twitter 架构优化之路--Twitter是如何做到每秒处理3000张图片的
如今,Twitter每秒可以创建并保存3000张(20GB)的图片.2015年,Twitter甚至从对媒体存储策略的优化中节省出了600万美元. 但并非一开始就是这样的,2012年Twitter还主要 ...
- 在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天。要求尽早聚会
在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会.骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天.要求尽早聚会 ...
- Java异常处理-----抛出处理
抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...
- 【伯乐在线】FACEBOOK产品设计总监:设计APP时的14个必考题
最近看到Facebook产品设计总监Julie Zhuo拷问产品的14个问题,非常靠谱.其中有3个问题堪称致命拷问: 1.使用前:这款产品是如何吸引到你的注意力的?2.开始使用:使用这款产品是轻松愉悦 ...
- Spring+EhCache缓存实例(详细讲解+源码下载)
一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...
- 你知道如何为iOS工程改名吗?
我们在iOS开发中,难免会遇到项目做到一半要改名字的情况.如果项目名差的太大,工程名看起来总是不舒服的,有良心的开发者可能就会想着为工程改个贴切的名字,那么你就为用到本文记录的内容. 如果我们开发的两 ...
- Android简易实战教程--第三十一话《自定义土司》
最近有点忙,好几天不更新博客了.今天就简单点,完成自定义土司. 主布局文件代码: <RelativeLayout xmlns:android="http://schemas.andro ...
- UE4 读取本地图片
参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-ht ...