今天用到了Scrolliew,从网上查了一下,有几点疑问,求前辈们给予指导,谢谢!
疑问一: 
         现象描述:xml中配置ScrollView,直接在其下面加一个系统的组件:例如,TextView或者Button等都是可以显示的。
但是换成自定义的View就不能显示(代码如下)。
          <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/ScrollView01"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:scrollbars="vertical">
                  <com.TestLayout.ResultDetailsView
                     android:layout_width="800px"
                    android:layout_height="400px">
                  </com.TestLayout.ResultDetailsView>
             </ScrollView> 
不知道什么原因,求指导,谢谢!
疑问二:
       现象描述:xml中配置ScrollView,不在下面直接放自定义View了,而是先放个LinearLayout,然后在LinearLayout中再放自定义View,这样如果自定义View中属性:android:layout_width和android:layout_height设定的是固定的值,则该自定义View始可以显示的。如果用的是:android:layout_width="match_parent"和android:layout_height="match_parent"则无法显示:(代码如下:)
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">
      <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
           <com.TestLayout1.ResultDetailsView
          android:layout_width="800px"
          android:layout_height="400px">
         </com.TestLayout1.ResultDetailsView>  
    </LinearLayout> 
     </ScrollView> 
这样始可以显示的,但是如果将
<com.TestLayout1.ResultDetailsView
          android:layout_width="800px"
          android:layout_height="400px">
 </com.TestLayout1.ResultDetailsView> 
改成:
<com.TestLayout1.ResultDetailsView
          android:layout_width="match_parent"
          android:layout_height="match_parent">
</com.TestLayout1.ResultDetailsView> 
就显示不出来。
求指导,谢谢!谢谢!

解决方案 »

  1.   

    疑问一:自定义的view需要另外的构造函数,view(参数一,参数儿),这个构造函数才是用来将自定义组件引用xml不居中的构造函数,可以网上找一下,的后事要重写这个函数。
      

  2.   

    第二个疑问,额,和你的自定义view可能有关系吧,所有部件都是填充parent,view里面没有实质性的东西就没得显示了。
      

  3.   

      谢谢,小哥!不过好像不是这个原因:我其中有View两个参数的构造函数。如果在疑问一的代码中将外层的ScrollView变成LinearLayout,自定义的View是可以显示的。
      还有疑问二好像也不是,因为自定义View中有好多TextView和Button的。
    谢谢 啊!
      

  4.   

    这个问题你最好贴一下你的com.TestLayout1.ResultDetailsView的代码哦。因为你的xml都米有问题,那肯定问题就出在你的自己一的view中有问题了。还有xml的宽度什么的以后最好用dip。不要用px了
      

  5.   

    哈哈,我也试了下,由于绘制view的时候会涉及测量和布局两个过程,估计是自定义的view重写onMeasure()方法不够完善造成的。
      

  6.   

    如果在onMeasure()中实现的setMeasuredDimension()函数给以确定的值就能显示了。想这样的布局代码也可以显示
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:scrollbars="vertical">
    <com.fm.TestForCsdn.MyView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="ajaskjdk" />
    </ScrollView>  
      

  7.   

      谢了,平小哥!
    自定义View的代码:我又写了一个很简单的(谅解com.TestLayout1.ResultDetailsView这个不敢贴,你懂的,哈哈),继承一个ViewGroup,里面addView()了一个button和一个TextView。
     代码如下:
    package com.TestLayout1;import android.content.Context;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;public class MyView1 extends ViewGroup {    private Button mButton = null;
        
        private TextView mTextView = null;
        
        private int mWidth;
        
        private int mHeight;
        
        public MyView1(Context context) {
            this(context, null, 0);
        }
        
        public MyView1(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
        
        public MyView1(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            
            mButton = new Button(context);
            mButton.setText("谢谢,平小哥!");
            addView(mButton);
            
            mTextView = new TextView(context);
            mTextView.setText("!!!!!!!!!!!!!!!!!!!!!!!");
            addView(mTextView);
        }
        
            @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            mWidth = getMeasuredWidth();
            mHeight= getMeasuredHeight();
        }    @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            // TODO Auto-generated method stub
            int left, top, right, bottom;
            left = 0;
            top = 0;
            right = mWidth / 2;
            bottom =  mHeight / 2;
            mButton.layout(left, top, right, bottom);
            
            top = bottom;
            right = mWidth;
            bottom = mHeight * 3 / 4;
            mTextView.layout(left, top, right, bottom);    }}
    然后在xml中用,还是出现同样的问题!不知道怎么解决,谢谢啊!
    PS:看到你回帖,我就开始写上面这个自定义view,结果一放到xml中就报这个错:The specified child already has a parent. You must call removeView() on the child's parent first. 我就开始找原因,感觉这么简单的一个View应该没有错误,找了好久!原来是:我addView()里面全部写的是同一个Button的实例。悲剧啊,,,,,,见笑了!哈哈!还有可能不是自定义View的问题。
      将自定义View在xml中不套scrollview直接放在LinearLayout中,然后在Activity中加载是没有问题的。
      但是加上后就不显示。
      

  8.   

      谢谢,小哥,麻烦你了!谢谢指教啊。
    上面的意思是不是在自定义View的onMeasure()方法中加上setMeasuredDimension(200,200)?谢谢啊!~
      

  9.   

    小哥,我按上面的方法使了一下:代码如下(还是不可以)
    自定义View:添加了setMeasuredDimension(800,400)
      package com.TestLayout1;import android.content.Context;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;public class MyView1 extends ViewGroup {    private Button mButton = null;
        
        private TextView mTextView = null;
        
        private int mWidth;
        
        private int mHeight;
        
        public MyView1(Context context) {
            this(context, null, 0);
        }
        
        public MyView1(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
        
        public MyView1(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            
            mButton = new Button(context);
            mButton.setText("谢谢,平小哥!");
            addView(mButton);
            
            mTextView = new TextView(context);
            mTextView.setText("!!!!!!!!!!!!!!!!!!!!!!!");
            addView(mTextView);
        }
        
            @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            mWidth = getMeasuredWidth();
            mHeight= getMeasuredHeight();
            setMeasuredDimension(800, 400);
        }    @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            // TODO Auto-generated method stub
            int left, top, right, bottom;
            left = 0;
            top = 0;
            right = mWidth / 2;
            bottom =  mHeight / 2;
            mButton.layout(left, top, right, bottom);
            
            top = bottom;
            right = mWidth;
            bottom = mHeight * 3 / 4;
            mTextView.layout(left, top, right, bottom);    }}XML代码如下:
      <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/ScrollView01"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:scrollbars="vertical">
        <com.TestLayout1.MyView1
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
        </com.TestLayout1.MyView1>
      </ScrollView>最后在Activity中加载上面xml。没有显示,谢谢指教!
      

  10.   

    看了下你的代码,由于我也是跟着你的这两个帖子了解的,所以说的不透彻。在绘制view的这两个过程会分别涉及到onMeasure()方法和layout()方法,这两个都是protected权限级别的,所以他们使用的数据都是类里面处理好了的,而这里默认的测量数据应该是100X100,即setMeasuredDimension(100, 100)的所以会显示不出来。因此需要自己设置测量数据,你设置的是setMeasuredDimension(800, 400)。但是除了测量,还需要进行布局,就是layout,在这里,是函数layout()实现的,而你布局位置使用的依然在默认数据基础上。所以你把每个部件的layout函数的参数值改动一下。mTextView.layout(left, top, right, bottom);四个在数载你上面measure的宽长范围内设置具体数据,就应该能显示出来了。
      

  11.   

    那四个参数的意思是以坐上角为原点的X_START,Y_SATRT,X_END,Y_END的值。
      

  12.   


      谢谢啊,不过我在onMeasure()中打了一下Log输出了一下
    mWidth和mHeight,得到的值是:800和0,估计始这里的错误,不过不知到怎么改,谢谢啊!
      

  13.   

    这个还真不知道啊,要不你不从view继承了,继承一个完善了onMeasure()和layout()的系统已有的部件。
      

  14.   

    mHeight = this.getMeasuredHeight();// 高度获取不到。因为srocllview的高度木有。控件本身原因,你设置也没用。因为你的view在scrollview里面。所以高度获取不到总是0.你把srocllview换成别的控件就可以。如果你一定要用srocllview的话最好还是不要自定义控件了。至于你把你的view高度定死。还是显示不出来这个问题。应该就是viewgroup的问题,你重写linearlayout就没有这个问题。 自定义view我写的比较少,所以也不是很了解啊。建议你多看看API 。有时候这样做不行就换个思路。换种方法。