如何以编程方式设置 textview-s 及其属性? [英] How to programmatically set textview-s and their properties?

查看:28
本文介绍了如何以编程方式设置 textview-s 及其属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tablerow-s 以编程方式在滚动视图中填充表格布局.在 tablerows 中,我将数据放在 textviews 中.
现在我的问题是如何以编程方式(在 Java 中)设置这样的文本视图:

I'm programmatically populating a tablelayout in a scrollview with tablerow-s. In the tablerows I am putting the data in textviews.
Now my questyon is how to programmatically(in java) set textviews like this:

<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"
/>

推荐答案

最后我是这样处理的:

In the end I did it like this:

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_*" 变量中存储了 "table header cels" 的大小...


In the "w_*" variabiles is stored the size of the "table header cels"...

这篇关于如何以编程方式设置 textview-s 及其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆