Android在tableRows创建textViews programmitcally [英] android create textViews in tableRows programmitcally

查看:247
本文介绍了Android在tableRows创建textViews programmitcally的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同数量的元素,这三个名单

 列表<串GT; competitoinsIDs =新的LinkedList<串GT;();
清单<串GT;标记=新的LinkedList<串GT;();
清单<串GT; numOfQuestions =新的LinkedList<串GT;();

我希望把每个列表的第一个元素在tablerow的,然后在另一个tablerow的每个列表的第二个元素,你会帮我请,这是XML

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    < TableLayout
        机器人:ID =@ + ID / tlMarksTable
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>        <的TableRow
            机器人:ID =@ + ID / tableRow1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:填充=5dip
            机器人:weightSum =2>            <的TextView
                机器人:ID =@ + ID / tvMarksCompetitionID
                机器人:layout_weight =1
                机器人:文字=竞争
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>            <的TextView
                机器人:ID =@ + ID / tvMarksMarks
                机器人:layout_weight =1
                机器人:文字=标志
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>            <的TextView
                机器人:ID =@ + ID / tvMarksQuestionsNum
                机器人:layout_weight =1
                机器人:文字=问题
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>
        < /&的TableRow GT;
    < / TableLayout>< /&的FrameLayout GT;


解决方案

首先改变你的XML文件:

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    < TableLayout
        机器人:ID =@ + ID / tlMarksTable
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>    < / TableLayout>
    < /&的FrameLayout GT;

我们将添加动态的所有内容。

 公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    清单<串GT; competitoinsIDs =新的LinkedList<串GT;();
    清单<串GT;标记=新的LinkedList<串GT;();
    清单<串GT; numOfQuestions =新的LinkedList<串GT;(); //确保该列表包含数据,否则显示器将黑屏    TableRow.LayoutParams params1 =新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0F);
    TableRow.LayoutParams params2 =新TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    TableLayout TBL =(TableLayout)findViewById(R.id.tlMarksTable);
    对于(INT CTR = 0;&CTR LT; marks.size(); CTR ++)
    {
    //创建新tablerows和textviews
    连续的TableRow =新的TableRow(本);
    TextView的TXT1 =新的TextView(本);
    TextView的TXT2 =新的TextView(本);
    TextView的txt3 =新的TextView(本);
    //设置文本
    txt1.setText(competitoinsIDs.get(CTR));
    txt2.setText(marks.get(CTR));
    txt3.setText(numOfQuestions.get(CTR));
    txt1.setLayoutParams(params1);
    txt2.setLayoutParams(params1);
    txt3.setLayoutParams(params1);
    //在textviews必须添加到所创建的行
    row.addView(TXT1);
    row.addView(TXT2);
    row.addView(txt3);
    row.setLayoutParams(params2);
    tbl.addView(行);
    }}

i have these three lists with the same number of elements

List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();

i want to put the first element of each list in a tablerow , then the second element of each list in another tablerow, would you help me please, this is the xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:weightSum="2" >

            <TextView
                android:id="@+id/tvMarksCompetitionID"
                android:layout_weight="1"
                android:text="Competition"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksMarks"
                android:layout_weight="1"
                android:text="Marks"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksQuestionsNum"
                android:layout_weight="1"
                android:text="Questions"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

</FrameLayout>

解决方案

First change your XML file to:

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    </TableLayout>
    </FrameLayout>

We'll add all the content dynamically.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    List<String>competitoinsIDs = new LinkedList<String>();
    List<String>marks = new LinkedList<String>();
    List<String>numOfQuestions = new LinkedList<String>();

 //make sure that the lists contain data or else display will be blank screen

    TableRow.LayoutParams  params1=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    TableRow.LayoutParams params2=new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    TableLayout tbl=(TableLayout) findViewById(R.id.tlMarksTable);
    for(int ctr=0;ctr<marks.size();ctr++)
    {
    //Creating new tablerows and textviews
    TableRow row=new TableRow(this);
    TextView txt1=new TextView(this);
    TextView txt2=new TextView(this);
    TextView txt3=new TextView(this);
    //setting the text
    txt1.setText(competitoinsIDs.get(ctr));
    txt2.setText(marks.get(ctr));
    txt3.setText(numOfQuestions.get(ctr));
    txt1.setLayoutParams(params1);
    txt2.setLayoutParams(params1);
    txt3.setLayoutParams(params1);
    //the textviews have to be added to the row created
    row.addView(txt1);
    row.addView(txt2);
    row.addView(txt3);
    row.setLayoutParams(params2);
    tbl.addView(row);
    }

}

这篇关于Android在tableRows创建textViews programmitcally的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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