在动态添加的行中更改文本

时间:2013-08-21 10:29:51

标签: android textview tablerow android-tablelayout

iam在Android中真的很新,并得到了一些问题:

我有一些TableLayout,在这里我想动态添加一些行。行必须看起来一样,所以我在xml中编写了一个模板,并将其作为视图添加到行中。

在这个xml文件中,有一些TextViews,现在我想在每个TableRow中更改这些TextViews的Text,以便每个表行都有其他文本,如星期日,星期一等。

后来我想从数组中添加这个Text。

TableLayoute xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/SPone_RL1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@drawable/backrepeat"
android:layout_gravity="center_vertical">

 <ImageView
          android:id="@+id/SPone_RL_ImageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:contentDescription="@drawable/logo"
          android:layout_centerInParent="true"
            />

<ScrollView 
    android:id="@+id/SPone_RL_SV"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" >

<RelativeLayout
    android:id="@+id/SPone_RL_SV_RL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TableLayout android:id="@+id/SPoneTable"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:padding="10dip" android:stretchColumns="*">

        </TableLayout>

 </RelativeLayout>

</ScrollView>
</RelativeLayout>

The Row xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout 
        android:id="@+id/SPone_RL_SV_RL_RL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

      <LinearLayout
            android:id="@+id/SPone_RL_SV_RL_RL_LL1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
             >

            <LinearLayout
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:orientation="vertical" 
                android:layout_marginLeft="20dp">

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="12"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/white"
                    android:textSize="25sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical" 
                android:layout_marginLeft="20dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:text="Sonntag"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/SPone_RL_SV_RL_RL_LL2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/SPone_RL_SV_RL_RL_LL1" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginLeft="70dp" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="25.08.2013"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginLeft="20dp" >

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="15:00"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/SPone_RL_SV_RL_RL_LL3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/SPone_RL_SV_RL_RL_LL2"
             >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="FC Bergalingen - FC Binzgen I"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/white"
                android:textSize="18sp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/SPone_RL_SV_RL_RL_LL4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/SPone_RL_SV_RL_RL_LL3"
             >

            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="0:6"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/white"
                android:textSize="30sp" />

        </LinearLayout>

    </RelativeLayout>

 </LinearLayout>

Java代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.fcbonespielplan);


    /* Find Tablelayout defined in main.xml */
 TableLayout tl = (TableLayout) findViewById(R.id.SPoneTable);


    for(int i = 0; i < 10; i++) {

        /* Create a new row to be added. */
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        /* Create a View to be the row-content. */
        LayoutInflater layoutInflater = (LayoutInflater) 
                this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                //das Layout das 10 mal rein soll
                ViewGroup yourview = (ViewGroup)layoutInflater.inflate(R.layout.spielplantablerow, null);



        /* Add View to row. */
        tr.addView(yourview);
        /* Add row to TableLayout. */
        tr.setBackgroundResource(R.drawable.baggroundtable);
        tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));





    }
}

任何人都可以帮助我每次如何设置其他文本,也许是其中一个TextViews?没有从Java生成整个TextView?

1 个答案:

答案 0 :(得分:0)

只需获取对TextView的引用并编辑如下文本:

TextView tv = (TextView) yourview.findViewById(R.id.textViewID); // if your TextView is in yourview. If not, change it.
tv.setText("Your text or text resource");