【问题标题】:Is there a way to lock a row in a scrollable tableLayout?有没有办法在可滚动的 tableLayout 中锁定一行?
【发布时间】:2020-04-16 20:58:34
【问题描述】:

我正在尝试在 AndroidStudio 中做一个 scollable tableLayout,我已经制作了 xml 代码以使其可滚动,我需要做的是锁定表格的第一行。

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".activity_storico_partite"
android:id="@+id/activity">
<Button
    android:id="@+id/btn_back"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="10dp"
    android:background="@color/transparent"
    android:foreground ="@drawable/ic_arrow_back_black_24dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tx_titolo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:text="Storico partite \n terminate"
    android:textSize="30dp"
    android:textAlignment="center"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:gravity="center_horizontal" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_span="4"
    android:layout_weight="1"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tx_noPartiteGiocate">

    <TableLayout
        android:id="@+id/tableDati"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textAlignment="center"
      >

        <TableRow
            android:id="@+id/tb_fisso"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp">

            <TextView
                android:id="@+id/tx_nomeGiocatore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="GIOCATORE"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_difficolta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="DIFFICOLTA'"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_tempo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:gravity="center_horizontal"
                android:text="TEMPO"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tx_punteggio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:gravity="center_horizontal"
                android:text="PUNTI"
                android:textAlignment="center"
                android:textColor="@color/Black"
                android:textSize="16dp" />

        </TableRow>

        <TableRow android:layout_marginBottom="20dp">

            <View
                android:id="@+id/separatore"
                android:layout_width="wrap_content"
                android:layout_height="2dp"
                android:layout_span="5"
                android:layout_weight="1"
                android:background="@color/CobaltBlue"
                android:gravity="center_horizontal"
                android:textAlignment="center" />
        </TableRow>


    </TableLayout>
</ScrollView>

<TextView
    android:id="@+id/tx_noPartiteGiocate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/Black"
    android:text="Nessuna partita"
    android:textSize="18dp"
    android:visibility="invisible"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tx_titolo" />

</androidx.constraintlayout.widget.ConstraintLayout>

注意我正在创建动态放入 tableLayout 中的行,因此如果您还需要该代码,只需将其写在注释中即可。

希望你能帮助我。

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    不,tablelayout 没有内置功能。您可以使用以下技巧。

    • 在 ScrollView 上方取另一个 TableLayout,只添加一个 tablerow。
    • 其余代码将保持不变,在 ScrollView 和 TablLayout 中

    如下所示。

    <TableLayout>
    <TableRow></TableRow> // this row will be frozen, will not scroll
    </TableLayout>
    
    <ScrollView>
    <TableLayout>
     // Add multiple tablerows in this table dynamically.
    </TableLayout>
    </ScrollView>
    

    并保持两个表的列奇偶校验使用下面的代码

    belowTable.post(new Runnable() {
        @Override
        public void run() {
            TableRow tableRow = (TableRow)belowTable.getChildAt(0);
            for(int i = 0; i < headerRow.getChildCount(); i++){
                headerRow.getChildAt(i).setLayoutParams(new TableRow.LayoutParams(tableRow.getChildAt(i).getMeasuredWidth(), tableRow.getChildAt(i).getMeasuredHeight()));
            }
        }
    });
    

    【讨论】:

    • 好的,如何在第二个 tableLayout 上设置与第一个相同的列宽?
    • @AlessandroVendrame 更新了两个表之间列宽奇偶性的答案
    • 明天我会告诉你它是否有效,提前谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多