zoukankan      html  css  js  c++  java
  • 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,子类就不说了,你可以自己去查文档。

    这条语句要放在继承类的构造函数中,如:

    [java] view plaincopy
     
    1. public classBackgroundLayout extendsLinearLayout {  
    2.   
    3.         publicBackgroundLayout(Context context, intposition) {  
    4.   
    5.                 super(context);  
    6.   
    7.                 // TODOAuto-generated constructor stub   
    8.   
    9.                 setWillNotDraw(false);  
    10.   
    11.         }  
    12.   
    13.         @Override  
    14.   
    15.         protected voidonDraw(Canvas canvas) {  
    16.   
    17.                 // TODOAuto-generated method stub   
    18.   
    19.                 super.onDraw(canvas);  
    20.   
    21.                 }  
    22.   
    23.         }  
    24.   
    25. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public classBackgroundLayout extendsLinearLayout {  
    2.   
    3.         publicBackgroundLayout(Context context, intposition) {  
    4.   
    5.                 super(context);  
    6.   
    7.                 // TODOAuto-generated constructor stub  
    8.   
    9.                 setWillNotDraw(false);  
    10.   
    11.         }  
    12.   
    13.         @Override  
    14.   
    15.         protected voidonDraw(Canvas canvas) {  
    16.   
    17.                 // TODOAuto-generated method stub  
    18.   
    19.                 super.onDraw(canvas);  
    20.   
    21.                 }  
    22.   
    23.         }  
    24.   
    25. }  


     

    三,扩展看法:

    eoeandroid上面Little关于这条语句的看法是:

           设置view是否更改,如果开发者用自定义的view,重写ondraw()应该将调用此方法设置为false,这样程序会调用自定义的布局。

    在此引用一下。

           其实从这条语句的字面意思上可以看出:setWillNotDraw(false);就是设置将不绘画吗?你重写了onDraw()当然是要进行绘画了,所以应将此语句参数置为false.

  • 相关阅读:
    [Sql Server][原创]
    SQL Server T-SQL高级查询
    SQL 网文链接
    Epicor系统二次开发
    lambda表达式的变量作用域
    写一个正则表达式匹配手机号
    函数装饰器在类方法中的使用方法
    关于Django的session的使用 (装饰器版)
    Django ORM相关操作(2)
    Django ORM相关操作(1)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5500907.html
Copyright © 2011-2022 走看看