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 ...
随机推荐
- MySQL where 子句
MySQL where 子句 我们知道从MySQL表中使用SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是SQL ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- DISC社交风格测试题--老虎 孔雀 考拉 猫头鹰
凭直觉,迅速回答 "我是谁",而不是"我应该是谁,或我想我是谁". 1.关于人生观,我的内心其实是: A 希望能够有尽量多的人生体验,所以会有非常多样化的想法. ...
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...
- [BBS]搭建开源论坛之JForum富文本编辑器更换
本文作者:sushengmiyan 本文地址:http://blog.csdn.net/sushengmiyan/article/details/47866905 上一节我们将开发环境搭建完成,我们 ...
- Linux 性能监测:IO
磁盘通常是计算机最慢的子系统,也是最容易出现性能瓶颈的地方,因为磁盘离 CPU 距离最远而且 CPU 访问磁盘要涉及到机械操作,比如转轴.寻轨等.访问硬盘和访问内存之间的速度差别是以数量级来计算的,就 ...
- Android ocr识别文字介绍(文字识别)
最近在做身份证号码识别,在网上搜索的一番后发现目前开源的OCR中tesseract-ocr算是比较强大的了,它由HP于1985年到1995年间开发,后来由google直接负责,经过谷歌进一步开发后,目 ...
- ERROR: In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing
eclipse sdk从低版本切换到高版本sdk的时候 v7包会包这个错ERROR: In <declare-styleable> MenuView, unable to find ...
- Afinal加载网络图片及下载文件使用方法
Afinal快速开发框架使用起来非常方便,下面将讲解如何利用Afinal加载网络图片及下载文件: 先看效果图: 注意:使用Afinal前需添加Afinal的jar,可以在这里下载:http://dow ...
- Java基本语法-----java标识符
1什么是标识符 就是程序员在定义java程序时,自定义的一些名字,例如helloworld 程序里关键字class 后跟的Demo,就是我们定义的类名.类名就属于标识符的一种. 标识符除了应用在类名上 ...