setWillNotDraw和setFillViewport
Romain Guy write a little info about a ScrollView attribute that is missing from documentation : Android:fillViewport=”true” .
It must be set to ScrollView and has the following efect : when set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.
当你想让一个高度值不足scrollview的子控件fillparent的时候,单独的定义android:layout_height="fill_parent"是不起作用的,必须加上fillviewport属性,当子控件的高度值大于scrollview的高度时,这个标签就没有任何意义了。
一.引言:
想必大家以前也遇到过这个问题:出于项目的需要,我们有时需要新建一个直接或者间接继承View的类,以便复写View提供的onDraw()方法,但有时我们反而得不到我们想要的结果,今天就说一下onDraw()方法不被执行的解决方法。你可能也在onDraw()方法里面设置了断点或log,却发现程序并没有执行onDraw()方法,那么你需要在你直接或者间接继承View的类的构造函数中加入下面的语句:
setWillNotDraw(false);
二.解释:
那么加这条语句的作用是什么?先看API:
If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(Canvas) you should clear this flag.
本人外语基础不是很好,简要翻译一下,如果翻译的不好,不要扔砖啊,重复一句我的语言:要想象,没有了想象,世界会是什么样。嘿嘿:
如果在当前的view上面不做任何的绘制操作,需要设置这个标记以便将来的更好的需要,默认的,这个标记在View里是不设定的。但是像View的一些子类如ViewGroup是可以设定的,典型的,你如果复写了onDraw(Canvas)方法,你需要清除此标记。
那么正好,我们所实现的就是View的子类:LinearLayout,当然你也可以继承其他的子类如:
AbsoluteLayout,AdapterView<T extends Adapter>,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawer,子类就不说了,你可以自己去查文档。
这条语句要放在继承类的构造函数中,如:
- public classBackgroundLayout extendsLinearLayout {
- publicBackgroundLayout(Context context, intposition) {
- super(context);
- // TODOAuto-generated constructor stub
- setWillNotDraw(false);
- }
- @Override
- protected voidonDraw(Canvas canvas) {
- // TODOAuto-generated method stub
- super.onDraw(canvas);
- }
- }
- }
- public classBackgroundLayout extendsLinearLayout {
- publicBackgroundLayout(Context context, intposition) {
- super(context);
- // TODOAuto-generated constructor stub
- setWillNotDraw(false);
- }
- @Override
- protected voidonDraw(Canvas canvas) {
- // TODOAuto-generated method stub
- super.onDraw(canvas);
- }
- }
- }
三,扩展看法:
eoeandroid上面Little关于这条语句的看法是:
设置view是否更改,如果开发者用自定义的view,重写ondraw()应该将调用此方法设置为false,这样程序会调用自定义的布局。
在此引用一下。
其实从这条语句的字面意思上可以看出:setWillNotDraw(false);就是设置将不绘画吗?你重写了onDraw()当然是要进行绘画了,所以应将此语句参数置为false.
setWillNotDraw和setFillViewport的更多相关文章
- Android -- setWillNotDraw()
干货 处理onDraw()方法不被执行的解决方法: setWillNotDraw(false); 官方文档的解释: If this view doesn't do any drawing on its ...
- ViewPager With FragmentPagerAdapter
采用PagerAdapter中的FragmentPagerAdapter来实现页面切换,适用于a handful of typically more static fragments to be pa ...
- Toolbar的使用
项目来源: https://github.com/xuwj/ToolbarDemo#userconsent# 一.V7包升级问题 折腾好久,终于解决 <style name="AppT ...
- ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现
注意:有关Fragment的方法和ViewPager的全部是android.support.v4包的,否则会报很多的错误 MainActivity: package com.zzw.fragmentt ...
- ViewPager实现页卡的3种方法(谷歌组件)
----方法一:---- 效果图: 须要的组件: ViewPager+PagerTabStrip 布局文件代码: <!--xmlns:android_custom="http://sc ...
- 【Android开源库】 PagerSlidingTabStrip从头到脚
简介 PagerSlidingTabStrip,是我个人经常使用到的一个和ViewPager配合的页面指示器,可以满足开发过程中常用的需求,如类似于今日头条的首页新闻内容导航栏等等,之前自己开发的Ju ...
- <Android开源库> PagerSlidingTabStrip从头到脚
简介 PagerSlidingTabStrip,是我个人经常使用到的一个和ViewPager配合的页面指示器,可以满足开发过程中常用的需求,如类似于今日头条的首页新闻内容导航栏等等,之前自己开发的Ju ...
- Fragment利用ViewPager实现左右滑动--第三方开源--SlidingTabLayout和SlidingTabStrip实现
MainActivity: package com.zzw.fragmentteb; import java.util.ArrayList; import android.graphics.Color ...
- FlycoTabLayout 从头到脚
简介 FlycoTabLayout,是一个比Google原生TabLayout 功能更强大的TabLayout库.目前有3种TabLayout: SlidingTabLayout CommonTabL ...
随机推荐
- Objective-C学习篇03—继承
大纲: 继承的基本概念 自定义初始化方法 便利构造器方法 重写description方法 一 继承基本概念 程序里的对象和"人类"的对象是一样的,高富帅继承了父母,自然就拥有了父母 ...
- java中的浮点计算
记得很久以前编写测试用例,无意间发现 double c=2.31;double d=0.1; System.out.println(c/d); 最后输出的竟然是23.099999999999998,而 ...
- IE8’s Substr() Bug
IE8不支持substr()函数, 第一个参数为负数,比如:var index = id.substr(-1, 1);替代:var index = id.substr(id.length-1, 1);
- 快速排序(Quick Sort)的C语言实现
快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...
- hdu 4548 第六周H题(美素数)
第六周H题 - 数论,晒素数 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- Linux文本操作三大利器总结:sed、awk、grep
grep:(去除一行中需要的信息,同类与cut) grep全称是Global Regular Expression Print #常规用法 # grep -n root /etc/passwd :ro ...
- NOIP[2015] 运输计划
传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...
- 编写优美的GTest测试案例
http://www.cnblogs.com/coderzh/archive/2010/01/09/beautiful-testcase.html 使用gtest也有很长一段时间了,这期间也积累了一些 ...
- SPSS与聚类分析
1.进行K均值聚类分析时需要线标准化处理,抛弃量纲差异,比如说数值型变量有的以千记有的以百分数记.2.层次聚类就是先把每个样本都看成一个独立的类:聚类特征(Clustering Feature, CF ...
- select m objects from n objects randomly
Q: how to select m objects randomly from n objects with euqal possibility? A: allocate an array of m ...