Android ScrollView with RelativeLayout

2019-07-15 16:50发布

I have a fragment with two RelativeLayout, one inside the other. This fragment is inside a tab. I want that the internal layout(layoutInternal) is scrollable, but with the following code does not work. I create the view object into the layoutInternal dinamically. Where is my mistake?

<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>

The following code is where i insert the view object (table) inside the intenalLayout.

    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));
    }

This is the screenshot of my app. enter image description here

1条回答
beautiful°
2楼-- · 2019-07-15 17:20

The RelativeLayout layoutInternal has the height initially set as android:layout_height="wrap_content" but its content is initially empty because the content is created dynamically; for this reason is not scrollable.

To solve this, when you finish to draw the tables into RelativeLayout layoutInternal, you have to set its height dynamically as below:

    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutInternal.getLayoutParams();
    params.height =  max_y_coordinate_used;
    layoutInternal.setLayoutParams(params);
查看更多
登录 后发表回答