当scrollview大于屏幕时,不会显示添加到水平滚动视图的自定义视图

时间:2014-06-20 15:55:45

标签: android android-layout

我真的希望你能提供帮助!我现在已经多次撞到墙上了。

我的应用程序的中心由线性布局组成,分为两部分。顶部布局是水平滚动视图。此滚动视图包含一个相对布局,该布局依次保持视图“theLine” - 只是屏幕上的一条线。

动态地,我将图像按钮添加到相对于theLine的相对布局。这些图像按钮通过“DrawArrow”连接,这是一个自定义视图类。这只会创建一条贯穿按钮的线。

一切正常,直到滚动视图变为“激活” - 即添加的图像按钮的宽度对于屏幕来说太大。然后,“theLine”消失,“DrawArrows”消失。图像按钮不会消失。我注意到“DrawArrows”中的onDraw没有被调用。

我不知道为什么,我真的可以帮忙。

适用的XML

<LinearLayout 
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_toRightOf="@id/ScrollVertical"
android:weightSum="100"
android:baselineAligned="false">

    <HorizontalScrollView
        android:id="@+id/scrollHorizontal"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:fillViewport="true"
        android:layout_weight="85"
        android:layout_gravity="center">

            <RelativeLayout
                android:id="@+id/actionFrame"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left">

                    <View
                        android:id="@+id/theLine"
                        android:layout_width="match_parent"
                        android:layout_height="2dip"
                        android:layout_centerInParent="true"
                        android:background="@drawable/linegradient"
                        android:padding="0dp">
                    </View> 

            </RelativeLayout>

    </HorizontalScrollView>     

Java Draw Arrow

public DrawArrow(Context context, View startView, View endView) {
    super(context);   
    paint.setStrokeWidth(5);
    paint.setStyle(Paint.Style.STROKE); 
    start = startView;
    end = endView;
}

public void onDraw(Canvas canvas) {

    paint.setColor(Color.BLACK);     
    canvas.drawLine(start.getX() + (start.getWidth()/2), start.getY()+ (start.getHeight()/2), end.getX() + (end.getWidth()/2), end.getY() + (end.getHeight()/2), paint);
}   

Java(添加了按钮/连接)

RelativeLayout container = (RelativeLayout) findViewById(R.id.actionFrame);
//Create button
container.addView(button);

DrawArrow connArr = new DrawArrow(this, findViewById(instruction.getPred().getId()), findViewById(currentPosition));  //Instruction is connected with button - they share an ID
container.addView(connArr);
}

注意:Button取决于它是第一个还是&gt;第一个添加,具有不同的布局参数。

所有都是WRAP_CONTENT,ALIGN_TOP / ALIGN_BOTTOM为“theLine”,如果是第一个,则为ALIGN_PARENT_LEFT,或者如果&gt;首先为RIGHT_OF,则为前一个按钮。

1 个答案:

答案 0 :(得分:2)

如果有人遇到此问题 -

我的问题是我没有覆盖DrawArrow中的onMeasure方法,所以一旦父布局改变了大小,由于滚动,DrawArrow被添加了0宽度。

我所要做的就是在其构造函数中将父布局传递给DrawArrow,然后在onMeasure中将setMeasuredDimension()传递给父宽度和大小。