2

我有一个带有两个RelativeLayout 的片段,一个在另一个里面。此片段位于选项卡内。我希望内部布局(layoutInternal)是可滚动的,但是使用以下代码不起作用。我动态地将视图对象创建到 layoutInternal 中。我的错误在哪里?

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutExsternal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="@dimen/fragmentHall_10dp_margin"
        android:layout_marginTop="@dimen/fragmentHall_10dp_margin">

        <RelativeLayout
            android:id="@+id/layoutInternal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </RelativeLayout>

    </ScrollView>   
</RelativeLayout>

以下代码是我在内部布局中插入视图对象(表)的位置。

    while (iteratorTables.hasNext()) {
        [...] //Here i calculate the dimension and coordinates
        b_table = new ButtonTable(SelzApplication.getAppContext(),tableMap.getValue(), widthTable, heightTable);

        b_table.setX(Math.round((tableMap.getValue().getX())* Xratio));
        b_table.setY(Math.round((tableMap.getValue().getY())* Yratio));

        //add Touch Listeners to the button
        b_table.setOnLongClickListener(new OnLongTuchDragDrop());   
        b_table.setOnClickListener(new TableOnClick()); 


        layoutInternal.addView(b_table, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

这是我的应用程序的屏幕截图。 在此处输入图像描述

4

1 回答 1

1

RelativeLayout layoutInternal高度最初设置为,android:layout_height="wrap_content"但其内容最初为空,因为内容是动态创建的;因为这个原因是不可滚动的。

为了解决这个问题,当您完成将表格绘制到 中时RelativeLayout layoutInternal,您必须动态设置其高度,如下所示:

    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutInternal.getLayoutParams();
    params.height =  max_y_coordinate_used;
    layoutInternal.setLayoutParams(params);
于 2013-07-09T10:05:20.803 回答