1

我在 Android 中真的很新并且有一些问题:

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

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

后来我想从一个数组中添加这个文本。

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>

行 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?

4

1 回答 1

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");
于 2013-08-21T10:58:45.730 回答