簡體   English   中英

tableRow條目不顯示在屏幕上

[英]tableRow entries go off screen

我正在使用表格布局來顯示基於用戶規范的信息。 用戶將數據輸入數據庫,然后獲取該數據並將其顯示在表上,每行有3個textView條目。 問題是,如果用戶輸入的數據太長,它將離開屏幕並且不可見。 有任何想法嗎? 我用來執行此操作的代碼如下:

XML

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*"
    android:id="@+id/tl">
<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="1dp" >

        <TextView
            android:id="@+id/timeCol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#FF0000"/>

        ....

    </TableRow>          
</TableLayout>

Java的

 for (final String time : timesArray) 
    {
        i++;
        TableRow row1= new TableRow(this);

        event = db.getEvent(i, gameId);
        player = db.getPlayer(i, gameId);

        TextView timeRow = new TextView(this);
        TextView playerRow = new TextView(this);
        TextView eventRow = new TextView(this);

        timeRow.setText(time);
        timeRow.setTextColor(Color.WHITE);
        playerRow.setText(player);
        playerRow.setTextColor(Color.WHITE);
        eventRow.setText(event);
        eventRow.setTextColor(Color.WHITE);

        row1.addView(timeRow);
        row1.addView(playerRow);
        row1.addView(eventRow);

        tl.addView(row1);  
    }

如果數據太長,看起來像這樣 在此處輸入圖片說明

解!

將layout_width設置為0,然后根據您希望一列占用的屏幕數量將float_weight分配給一個浮點數(例如,如果0.5則該列占據整個屏幕的一半),則可以在屏幕上顯示所有信息。 下面是執行此操作的代碼。

XML

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tl">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="1dp" >

        <TextView
            android:id="@+id/timeCol"
            android:layout_width="0px"
            android:layout_weight="0.2"
            android:layout_height="wrap_content"
            android:text="Time"/>

        <TextView
            android:id="@+id/playerCol"
            android:layout_width="0px"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Player"/>

        <TextView
            android:id="@+id/eventCol"
            android:layout_width="0px"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Event"/>

    </TableRow>          

java的

TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableRow row1= new TableRow(this);

TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);

TableRow.LayoutParams text1Params = new TableRow.LayoutParams();
        text1Params.width = 0;
        text1Params.weight = (float) 0.2;
        TableRow.LayoutParams text2Params = new TableRow.LayoutParams();
        text2Params.weight = (float) 0.4;
        text2Params.width = 0;
        TableRow.LayoutParams text3Params = new TableRow.LayoutParams();
        text3Params.weight = (float) 0.4;
        text3Params.width = 0;

        timeRow.setLayoutParams(text1Params);
        playerRow.setLayoutParams(text2Params);
        eventRow.setLayoutParams(text3Params);

        row1.addView(timeRow);
        row1.addView(playerRow);
        row1.addView(eventRow);

        tl.addView(row1, params);  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM