2

我正在处理自定义视图。当我将自定义视图放在水平滚动视图中时,水平滚动不起作用。我认为我在编写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);

谁能帮我找到我做错的地方

提前致谢..

4

1 回答 1

-1

他们两个都工作:

在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>
于 2013-03-13T06:41:26.920 回答