如何以编程方式设置textview-s及其属性?

时间:2013-07-01 10:15:35

标签: android textview tablelayout

我以编程方式在tablerow-s的scrollview中填充tablelayout。在表格中,我将数据放在textviews中 现在我的问题是如何以编程方式(在java中)设置像这样的文本视图:

<TextView
    android:layout_width="20dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/tab"
    android:layout_weight="0.4"
    android:minWidth="80dp"
/>

<TextView
    android:layout_width="81dp"
    android:layout_height="wrap_content"
    style="@style/tab"
/>

<TextView
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    style="@style/tab"
/>

<TextView
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center"
    style="@style/tab"
    android:textStyle="bold"
    android:textColor="@color/black"
    android:layout_marginRight="5dp"
/>

2 个答案:

答案 0 :(得分:7)

最后我这样做了:

TextView tvID = new TextView(this);
    tvID.setLayoutParams(new TableRow.LayoutParams(w_id, TableRow.LayoutParams.WRAP_CONTENT));
        tvID.setPadding(5, 0, 0, 0);
    tvID.setTextSize(11);
    tvID.setText(sharedIDArt);
artikliVrstica.addView(tvID);


TextView tvNazArt = new TextView(this);
    TableRow.LayoutParams tvNazArtParms = new TableRow.LayoutParams(w_naz, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
        tvNazArtParms.setMargins(5, 0, 0, 0);
    tvNazArt.setLayoutParams(tvNazArtParms);
    tvNazArt.setTextAppearance(this, android.R.style.TextAppearance_Large);
    tvNazArt.setText(NazivArt);
artikliVrstica.addView(tvNazArt);


TextView tvKol = new TextView(this);
    TableRow.LayoutParams tvKolParms = new TableRow.LayoutParams(w_kol, TableRow.LayoutParams.WRAP_CONTENT, 0.3f);
        tvKolParms.setMargins(5, 0, 0, 0);
    tvKol.setLayoutParams(tvKolParms);
    tvKol.setTextAppearance(this, android.R.style.TextAppearance_Large);
    tvKol.setGravity(Gravity.RIGHT);
    tvKol.setText(kolicina);
artikliVrstica.addView(tvKol);


TextView tvCena = new TextView(this);
    TableRow.LayoutParams tvCenaParms = new TableRow.LayoutParams(w_cen, TableRow.LayoutParams.WRAP_CONTENT, 0.3f);
        tvCenaParms.setMargins(5, 0, 10, 0);
    tvCena.setLayoutParams(tvCenaParms);
    tvCena.setTextAppearance(this, android.R.style.TextAppearance_Large);
    tvCena.setGravity(Gravity.RIGHT);
    tvCena.setText(cena);
artikliVrstica.addView(tvCena);


TextView tvIzbris = new TextView(this);
    TableRow.LayoutParams tvIzbrParms = new TableRow.LayoutParams((w_izb-10), TableRow.LayoutParams.WRAP_CONTENT);
        tvIzbrParms.setMargins(10, 0, 5, 0);
    tvIzbris.setLayoutParams(tvIzbrParms);
    tvIzbris.setPadding(0, 7, 10, 7);
    tvIzbris.setTextAppearance(this, android.R.style.TextAppearance_Large);
    tvIzbris.setTypeface(null,Typeface.BOLD);
    tvIzbris.setTextColor(Color.BLACK);
    tvIzbris.setBackgroundColor(Color.WHITE);
    tvIzbris.setGravity(Gravity.CENTER);
    tvIzbris.setText("-");
artikliVrstica.addView(tvIzbris);


在“w_ *”变量中存储“表格标题”的大小......

答案 1 :(得分:1)

试试这个,它可能会有所帮助

    TextView view = new TextView(this);
    view.setText("example textview ");
   //adding layout properties 
    view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
   // add the textview to the parentLayout
    parentLayout.addView(view);

相关链接: Dynamically add textViews to a linearLayout

how to generate dynamic textview in android?

Dynamically Add A TextView - Android