Android ScrollView with RelativeLayout

风流意气都作罢 提交于 2019-12-24 00:50:12

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/17464764/android-scrollview-with-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!