6

我有CustomComponent 扩展的类ViewGroup

源代码CustomComponent

      public class CustomComponent extends ViewGroup {

            private static final String LOGTAG = "CustomComponent";

            private List<MenuItem> items;

            private Context context;

            private int screenWidth;
            private int screenHeight;
            private int cellWidth;
            private int cellHeight;
            private int duration;
        private int space=7;

       public CustomComponent(Context context) {
            super(context);
            this.context=context;
       }


             @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Log.v(LOGTAG, "on Measure called");
             screenWidth = MeasureSpec.getSize(widthMeasureSpec);
              screenHeight = MeasureSpec.getSize(heightMeasureSpec);
              cellHeight=screenHeight/AppConstants.HEIDHTCELLSCOUNT;
              cellWidth=screenWidth/AppConstants.WIDTHCELLSCOUNT;
              duration= cellHeight*2;
             super.onMeasure(widthMeasureSpec, heightMeasureSpec+duration);

          }


           @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            Log.v(LOGTAG, "onLayout called");
            int childCount = this.getChildCount();
                 for (int i = 0; i < childCount; i++) {
                      View child = getChildAt(i);
                          child.layout(items.get(i).getLeft(),items.get(i).getTop(),items.get(i).getRight(), items.get(i).getBottom());
        } 
      }

public List<MenuItem> getItems() {
        return items;
    }

    public void setItems(List<MenuItem> items) {
        this.items = items;
    }
    }

xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"
    android:scrollbars="vertical">
        <<package name>.CustomComponentandroid:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/menu_component"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"/>
</LinearLayout>

我需要为此添加垂直滚动ViewGroup。请帮忙,我不知道如何解决这个问题。 在此处输入图像描述

4

3 回答 3

3

尝试这个。您需要在 ScrollView 中使用 LinearLayout。将 MenuComponent 放在 LinearLayout 中。它应该工作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <<package name>.MenuComponent android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:id="@+id/menu_component"
                android:scrollbars="vertical"
                android:fadingEdge="vertical"/>
        </LinearLayout>

    </ScrollView>
</LinearLayout>
于 2012-03-02T10:15:15.007 回答
1

您可以尝试将您MenuComponent的内容包括在内ScrollView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:id="@+id/menu_layout"
    android:scrollbars="vertical">
      <ScrollView android:id="@+id/ScrollView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" >

        <<package name>.MenuComponent android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/menu_component"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"/>
       </ScrollView>
</LinearLayout>
于 2012-03-01T11:54:21.663 回答
0

您必须调用awakenScrollBars(),这将触发滚动条在您的自定义视图组上绘制。(更多详细信息)并且启用的垂直滚动条必须为真setVerticalScrollBarEnabled()

您还必须覆盖函数computeVerticalScrollExtent()computeVerticalScrollRange设置拇指的大小和滚动条滚动范围。

于 2014-07-01T13:37:21.453 回答