• android开发中scrollview添加自定义view的滑动显示问题


    最近做了一个实战用到自定义view,由于view比屏幕大所以想放到scrollview中,如下程序。发现不显示。于是对scrollview进行了研究。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2 xmlns:tools="http://schemas.android.com/tools"
     3 android:layout_width="match_parent"
     4 android:layout_height="match_parent"
     5 tools:context=".MainActivity" 
     6 android:orientation="vertical"
     7 android:weightSum="35">
     8 
     9 <LinearLayout
    10 android:layout_width="match_parent"
    11 android:layout_height="0dp"
    12 android:layout_weight="2"
    13 android:orientation="horizontal"
    14 
    15 >
    16 </LinearLayout>
    17 <ScrollView
    18 android:id="@+id/scrollView1"
    19 android:layout_width="match_parent"
    20 android:layout_height="0dp" 
    21 android:fillViewport="true"
    22 android:layout_weight="30">
    23 
    24 <LinearLayout
    25 android:id="@+id/linearLayout1"
    26 android:layout_width="match_parent"
    27 android:layout_height="wrap_content" >
    28 
    29 <com.example.android_draw.Mydraw
    30 android:id="@+id/mydraw1"
    31 android:layout_width="wrap_content"
    32 android:layout_height="wrap_content" />
    33 
    34 </LinearLayout>
    35 </ScrollView>
    36 <LinearLayout
    37 android:layout_width="match_parent"
    38 android:layout_height="0dp"
    39 android:layout_weight="3"
    40 android:orientation="vertical" >
    41 </LinearLayout>
    42 
    43 </LinearLayout>

    理论部分

      1、ScrollView和HorizontalScrollView是为控件或者布局添加滚动条

      2、上述两个控件只能有一个孩子,但是它并不是传统意义上的容器

      3、上述两个控件可以互相嵌套

      4、滚动条的位置现在的实验结果是:可以由layout_width和layout_height设定

      5、ScrollView用于设置垂直滚动条,HorizontalScrollView用于设置水平滚动条:需要注意的是,有一个属性是    scrollbars 可以设置滚动条的方向:但是ScrollView设置     成horizontal是和设置成none是效果同,HorizontalScrollView设置成vertical和none的效果同。

      6. ScrollView要求其只有一个子View。当有多个View时,可以使用LinearLayout等布局包含,使其直接子View只有一个。

      7,scrollview会对其内部的view大小进行判断(长宽为零时可能不执行),以便给予显示空间,决定是否显示滑动条。

    所以在scrollview里添加自定义view时,我们给自定义view设置大小。

    方法一:在.xml文件直接设置

      

    29 <com.example.android_draw.Mydraw
    30 android:id="@+id/mydraw1"
    31 android:layout_width="wrap_content"
    32 android:layout_height="600dp" />

    方法二:在自定义view中 重写onMeasure方法。

     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)介绍:

      测量View及其Content,确定measuredWidth和measuredHeight。在方法measure(int, int)中调用。重写onMeasure方法时,需要调用方法setMeasuredDimension(int, int),存储View的measuredWidth和measuredHeight。若存储失败,方法measure(int, int)会抛出异常IllegalStateException。可以调用super.onMeasure(int, int)方法。

      除非MeasureSpec准许更大的size,否则measure的默认实现是background size。子类重写onMeasure(int, int)提供Content的更佳测量。如果onMeasure被重写,子类必须保证measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通过getSuggestedMinimumHight/Width()获取。

    参数width/heightMeasureSpec表示parent强加的horizontal/vertical space要求。

      在自定义view中写如下代码,

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        setMeasuredDimension(宽度, 高度);
        }

    结束。

  • 相关阅读:
    python三大神器之virtualenv pip, virtualenv, fabric通称为pythoner的三大神器。
    pip使用国内镜像,豆瓣、清华
    pip国内源
    Android原生(Native)C开发之四:SDL移植笔记
    libcurl使用easy模式阻塞卡死等问题的完美解决
    linux 自定义信号
    ubuntu 12.04安装telnet和ssh服务
    libcurl with telnet
    ubuntu使用ssh登入不执行.bashrc解决方法
    pthread_kill
  • 原文地址:https://www.cnblogs.com/jyycnblogs/p/5052192.html
Copyright © 2020-2023  润新知