簡體   English   中英

Android - 動態 ColSpan 不起作用

[英]Android - Dynamic ColSpan not working

我的一項活動中有兩個表格行。 第一行將始終包含 2 個文本視圖,第二行可能包含一個或兩個文本視圖(取決於用戶輸入)。 每當我在第二行有一個 textview 時,我都試圖將它跨越到兩列。 我正在嘗試這個

TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams();
param.span = 2;
txtView.setLayoutParams(param);

但它不起作用。 發生的情況是,第一行中的第二個 txtview 被推出屏幕。 那么這里的任何人都可以告訴我在哪里犯錯了嗎?

我做了這個例子,希望對您有幫助。

 TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
    TableRow row = new TableRow(this);
    TableRow row2 = new TableRow(this);
    TextView col1, col2, col3;        

    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    col1 = new TextView(this);
    col2 = new TextView(this);
    col3 = new TextView(this);

    col1.setText("col1");
    col2.setText("col2");
    col3.setText("col3");

    row.addView(col1);
    row.addView(col2);
    row.addView(col3);

    table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //HERE IS WHAT YOU NEED
    TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
    params.span = 3; //amount of columns you will span

    col1 = new TextView(this);

    col1.setText("span on col1 makes it bigger than the others");
    col1.setLayoutParams(params);
    row2.addView(col1);

    table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

解釋標記為正確的答案 - 看起來您需要在將視圖添加到其父級后設置布局參數。 例如,將 textview 添加到一行,然后設置布局參數

暫無
暫無

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

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