动态将片段放置在表格布局中

时间:2020-06-09 20:52:35

标签: java android android-fragments

我有一个小问题。我有n个片段,我想将这些片段连续放置,但是当屏幕用完时,其余片段将移至底线,依此类推。

我正在这样做:

   for (int i = 0; i < n ; i++) {
        PresencaFragment aFragment = new PresencaFragment("Test "+i, "2017059227_11.jpg", 2017000000);
        getSupportFragmentManager().beginTransaction().add(R.id.frag, aFragment).commit();
    }

我的片段和家庭片段的代码(我将这n个片段放在家庭片段中)是:

HomeFragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtHoras"
        android:layout_marginTop="125dp"
        android:focusable="true"
        android:text="A carregar dados..."
        android:textColor="@color/corescola"
        android:textSize="25dp"
        android:layout_height="40dp"
        android:layout_width="match_parent"
        android:textAlignment="center"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="145dp"
        android:layout_marginBottom="5dp">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frag"
            >

        </TableLayout>
    </ScrollView>
</RelativeLayout>

还有我的片段代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PresencaFragment">

    <TableLayout
        android:layout_width="391dp"
        android:layout_height="264dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="8dp"
        android:stretchColumns="1">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="252dp"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="10dp"
            android:stretchColumns="1">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:background="#7EC5CAE9"
                android:minHeight="55dp">

                <TextView
                    android:id="@+id/txt_nomeAluno"
                    android:layout_width="378dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="10dp"
                    android:width="300dp"
                    android:text="Student Name"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="24dp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#eee"
                android:minHeight="35dp">

                <TextView
                    android:id="@+id/txt_numero"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:width="300dp"
                    android:padding="10dp"
                    android:text="Student number: xxxxxxxxx"

                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="20dp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#eee"
                android:minHeight="35dp">

                <ImageView
                    android:id="@+id/imagem"
                    android:layout_width="371dp"
                    android:layout_height="match_parent"
                    tools:src="@tools:sample/avatars" />

            </TableRow>


        </TableLayout>

    </TableLayout>
</FrameLayout>

所以,我想要这个,但要得到这个:

image of what i want and what i obtain

1 个答案:

答案 0 :(得分:0)

开始片段时,您正在传递R.id.frag。据我所知,R.id.frag是整个表布局的ID,这意味着您正在用片段替换整个表。您想要的是在正确的行ID中设置每个片段。

注意:在表中包含碎片确实是一个坏习惯。如果您需要表格布局,则应使用其中的视图,并查看有关它的一些教程,例如:https://abhiandroid.com/ui/tablelayout