水平滚动视图内的自定义视图不滚动

时间:2013-03-13 06:00:52

标签: android android-view android-custom-view horizontalscrollview

我正在制作自定义视图。当我将自定义视图放在水平滚动视图中时,水平滚动不起作用。我认为我写错了 MeasureSpec ,但我不知道如何写一个 MeasureSpec 自定义视图

我的视图类代码就像这样

public class Makecell extends ViewGroup{
    private int line_height;  
    private int line_width;
    Context mContext;
    subview sub;
    int left,right,top,bottom;
    MainActivity main;
    public Makecell(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public Makecell(Context context, AttributeSet attrs){
        super(context, attrs);
    }

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

         final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
            int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();

            final int count = getChildCount();
            int line_height = 0;
            int line_width=0;

            int xpos = getPaddingLeft();  
            int ypos = getPaddingTop();
            for (int i = 0; i < count; i++) {
                 final View child = getChildAt(i);
                 if (child.getVisibility() != GONE) {

                     child.measure(
                             MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED),
                             MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));

                     final int childw = child.getMeasuredWidth();
                     final int childh = child.getMeasuredHeight();
                     line_height = Math.max(line_height, child.getMeasuredHeight());
                     line_width=Math.max(line_width, child.getMeasuredWidth());
                     if (xpos + childh > height) {
                         xpos = getPaddingLeft();
                         ypos += line_width; 
                     }
                     xpos += childw;
                 }
            }

            this.line_width=line_width;


    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

          final int count = getChildCount();
          final int width = r - l;
          final int hight = b - t;  
          int xpos = 0;
          int ypos = 0;

          for (int i = 0; i < count; i++) {
              final View child = getChildAt(i);
              if (child.getVisibility() != GONE) {
                  final int childw = child.getMeasuredWidth();
                  final int childh = child.getMeasuredHeight();
//                
                  if (xpos + childh > hight) {
                      xpos = getPaddingLeft();
                      ypos += line_width;


                  }
                child.layout(ypos, xpos,ypos + childw, xpos + childw );
                xpos += childh;

              }
          }
    }
}

和xml看起来像这样

<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"
    tools:context=".MainActivity" >
   <HorizontalScrollView 
       android:layout_height="match_parent"
       android:layout_width="match_parent"
       android:fillViewport="true"
       android:id="@+id/rootlin"
       > 
   </HorizontalScrollView>
</RelativeLayout>

在代码中添加视图看起来像

cell=new Makecell(getApplicationContext());
        mainlin=(HorizontalScrollView)findViewById(R.id.rootlin);
        LinearLayout lin=new LinearLayout(this);
        lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
        lin.setOrientation(LinearLayout.HORIZONTAL);
        for(int i=0;i<100;i++)
        {
            text=new TextView(this);
            text.setText("Text "+i);
            text.setTextSize(15);
            text.setBackgroundColor(Color.CYAN);
            text.setWidth(screenwidth/2);
            cell.addView(text);
        }
        lin.addView(cell);
        mainlin.addView(lin);

任何人都可以帮我找到我犯错的地方

提前感谢..

1 个答案:

答案 0 :(得分:0)

两者都有效:

在java中:

    LinearLayout lin=new LinearLayout(this);
    lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
    lin.setOrientation(LinearLayout.HORIZONTAL);
    lin.setHorizontalScrollBarEnabled(true);

并在xml文件中:

   <LinearLayout 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:scrollbars="horizontal"
      android:orientation="horizontal"
      tools:context=".MainActivity" >
   </LinearLayout>