簡體   English   中英

在Android layout_weight中獲取動態表中的表行

[英]in Android layout_weight for Table Rows in dynamic Table

我正在動態創建表行,並將兩個TextView添加到新行中。 這樣做可以正常工作,但是現在我正在嘗試格式化LayoutParams,以使兩個TextView的layout_weight均為1。 我嘗試添加一些LayoutParams,但看起來它們被忽略了。 這是我創建行的代碼,我注釋掉了LayoutParams位,運行應用程序時沒有任何變化,這告訴我LayoutParams被忽略了。 我需要一些幫助,將layout_weight分配給兩個TextView,以便在創建行的代碼中可以實現這一點

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1" />

所以這是我創建行的代碼:

        TableLayout tl = (TableLayout) findViewById(R.id.pres_table);

    for (int ctr = 0; ctr < pres_res.size(); ctr++)

    {
        final String pst_str = pres_res.get(ctr);
        final String yrs_str = yrs_res.get(ctr);

        TableRow tr = new TableRow(this);
        /*
        TableRow.LayoutParams trPara = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT);
        int leftMargin = 10;
        int topMargin = 2;
        int rightMargin = 10;
        int bottomMargin = 2;
        trPara.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trPara);

         */
        TextView tv1 = new TextView(this);
        tv1.setText(pst_str);
        tv1.setTextColor(Color.BLACK);
        tv1.setTextSize(18);

        TextView tv2 = new TextView(this);
        tv2.setText(yrs_str);
        tv2.setTextColor(Color.BLACK);
        tv2.setTextSize(18);

        tr.addView(tv1);
        tr.addView(tv2);
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

創建表的XML基本如下:

            <TableLayout
            android:id="@+id/pres_table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF" >
        </TableLayout>

謝謝!

// try this way
        TextView tv1 = new TextView(this);
        tv1.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
        tv1.setText(pst_str);
        tv1.setTextColor(Color.BLACK);
        tv1.setTextSize(18);

        TextView tv2 = new TextView(this);
        tv2.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
        tv2.setText(yrs_str);
        tv2.setTextColor(Color.BLACK);
        tv2.setTextSize(18);

        tr.addView(tv1);
        tr.addView(tv2);
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));

首先,感謝Geobits將我設置在正確的路徑上,並讓我知道我在錯誤的位置調用了LayoutParams。 它們需要應用於TextView,而不是應用於Table Row。 然后基於此,我發現了一個StackOverflow發布: 以編程方式設置TextView的布局權重,第二個答案是我的解決方案。 所以現在我的代碼看起來像這樣,並且可以按照我想要的方式工作:

        TableLayout tl = (TableLayout) findViewById(R.id.pres_table);

    for (int ctr = 0; ctr < pres_res.size(); ctr++)

    {
        final String pst_str = pres_res.get(ctr);
        final String yrs_str = yrs_res.get(ctr);

        TableRow tr = new TableRow(this);
        /*
        TableRow.LayoutParams trPara = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT);
        int leftMargin = 10;
        int topMargin = 2;
        int rightMargin = 10;
        int bottomMargin = 2;
        trPara.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trPara);
        tr.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

         */
        LayoutParams tvPar = new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT, 1f);
        TextView tv1 = new TextView(this);
        tv1.setLayoutParams(tvPar);
        tv1.setText(pst_str);
        tv1.setTextColor(Color.BLACK);
        tv1.setTextSize(18);

        TextView tv2 = new TextView(this);
        tv2.setLayoutParams(tvPar);
        tv2.setText(yrs_str);
        tv2.setTextColor(Color.BLACK);
        tv2.setTextSize(18);

        tr.addView(tv1);
        tr.addView(tv2);
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

解決方案的正下方是顯示新的LayoutParams tvPar代碼的解決方案。

暫無
暫無

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

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