xml-layouts laden und dynamisch benutzen?

  • Antworten:1
Marco S.
  • Forum-Beiträge: 105

31.03.2010, 07:46:16 via Website

guten morgen,

ich hab da ein szenario, was ich erstmal als etwas unbefriedigend empfinde.

1) ich hab ein layout.xml mit tablelayout

2) ich möchte dynamisch tablerows hinzufügen

3) also habe ich mir ein row.xml gebaut, und da ich nicht wusste wie man das dynamisch lädt und benutzt, habe ich das ganze per hand in java umgesetzt

4) das hat den nachteil, dass ein layouten etwas schwieriger ist, bzw umständlicher...

gibts eine möglichkeit die row.xml zu laden, mittels getViewById die einzelnen Komponenten zu verändern (hauptsächlich getText), und dann dem layout hinzuzufügen?

das layout.xml:
1<?xml version="1.0" encoding="utf-8"?>
2<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/spiel" android:layout_width="fill_parent"
4 android:layout_height="fill_parent" android:background="#ffffff"
5 android:orientation="horizontal" android:stretchColumns="0,1,2">
6
7</TableLayout>

das row.xml:
1<TableRow android:layout_width="fill_parent"
2 android:layout_height="wrap_content" android:paddingTop="5dip"
3 android:paddingBottom="5dip">
4 <TextView android:layout_width="wrap_content"
5 android:layout_height="wrap_content" android:text="VfL Bochum"
6 android:layout_gravity="center_horizontal" android:textSize="15dip"
7 android:textColor="#222222" android:textStyle="bold" android:id="@+id/homeName" />
8 <TextView android:layout_width="wrap_content"
9 android:layout_height="wrap_content" android:text="-"
10 android:textSize="15dip" android:textColor="#222222"
11 android:textStyle="bold" android:layout_gravity="center_horizontal" />
12 <TextView android:layout_width="wrap_content"
13 android:layout_height="wrap_content" android:text="Eintracht Frankfurt"
14 android:layout_gravity="center_horizontal" android:textSize="15dip"
15 android:textColor="#222222" android:textStyle="bold" android:id="@+id/guestName" />
16 </TableRow>
17
18 <TableRow android:layout_width="fill_parent"
19 android:layout_height="wrap_content" android:paddingTop="5dip"
20 android:paddingBottom="10dip">
21 <ImageView android:src="@drawable/fb_vflbochum"
22 android:scaleType="centerInside" android:layout_height="60dip"
23 android:layout_width="60dip" android:layout_gravity="center"
24 android:id="@+id/homeIcon" />
25 <TextView android:layout_width="wrap_content"
26 android:layout_height="wrap_content" android:text="1:2"
27 android:textColor="#222222" android:textStyle="bold"
28 android:textSize="20dip" android:layout_gravity="center" android:id="@+id/finalResult" />
29 <ImageView android:src="@drawable/fb_eintrachtfrankfurt"
30 android:layout_height="60dip" android:layout_width="60dip"
31 android:scaleType="centerInside" android:layout_gravity="center"
32 android:id="@+id/guestIcon" />
33 </TableRow>

das row.xml in java nachgebaut:
1public void createGame(TableLayout tl, String homeName, int homeIcon,
2 String guestName, int guestIcon, String finalResult, boolean gray ) {
3
4 TableRow tr10 = new TableRow(this);
5 TableLayout.LayoutParams tlp10 = new TableLayout.LayoutParams();
6 tlp10.width = TableLayout.LayoutParams.FILL_PARENT;
7 tlp10.height = TableLayout.LayoutParams.WRAP_CONTENT;
8 tr10.setLayoutParams(tlp10);
9 tr10.setPadding(0, 5, 0, 5);
10 if( gray ) {
11 tr10.setBackgroundColor(Color.rgb(0xF5, 0xF5, 0xF5));
12 }
13
14 TextView tv11 = new TextView(this);
15 TableRow.LayoutParams trp11 = new TableRow.LayoutParams();
16 trp11.width = TableRow.LayoutParams.WRAP_CONTENT;
17 trp11.height = TableRow.LayoutParams.WRAP_CONTENT;
18 trp11.gravity = Gravity.CENTER_HORIZONTAL;
19 tv11.setLayoutParams(trp11);
20 tv11.setTextSize(15);
21 tv11.setText(homeName);
22 tv11.setTextColor(Color.rgb(0x22, 0x22, 0x22));
23 tv11.setTypeface(Typeface.DEFAULT_BOLD);
24 tr10.addView(tv11);
25
26 TextView tv12 = new TextView(this);
27 TableRow.LayoutParams trp12 = new TableRow.LayoutParams();
28 trp12.width = TableRow.LayoutParams.WRAP_CONTENT;
29 trp12.height = TableRow.LayoutParams.WRAP_CONTENT;
30 trp12.gravity = Gravity.CENTER_HORIZONTAL;
31 tv12.setLayoutParams(trp12);
32 tv12.setTextSize(15);
33 tv12.setText("-");
34 tv12.setTextColor(Color.rgb(0x22, 0x22, 0x22));
35 tv12.setTypeface(Typeface.DEFAULT_BOLD);
36 tr10.addView(tv12);
37
38 TextView tv13 = new TextView(this);
39 TableRow.LayoutParams trp13 = new TableRow.LayoutParams();
40 trp13.weight = TableRow.LayoutParams.WRAP_CONTENT;
41 trp13.height = TableRow.LayoutParams.WRAP_CONTENT;
42 trp13.gravity = Gravity.CENTER_HORIZONTAL;
43 tv13.setLayoutParams(trp13);
44 tv13.setTextSize(15);
45 tv13.setText(guestName);
46 tv13.setTextColor(Color.rgb(0x22, 0x22, 0x22));
47 tv13.setTypeface(Typeface.DEFAULT_BOLD);
48 tr10.addView(tv13);
49
50 tl.addView(tr10);
51
52 ////////////////////////////////////////////////////////////////////////////////////////////
53
54 TableRow tr20 = new TableRow(this);
55 TableLayout.LayoutParams tlp20 = new TableLayout.LayoutParams();
56 tlp20.width = TableLayout.LayoutParams.FILL_PARENT;
57 tlp20.height = TableLayout.LayoutParams.WRAP_CONTENT;
58 tr20.setLayoutParams(tlp20);
59 tr20.setPadding(0, 5, 0, 10);
60 if( gray ) {
61 tr20.setBackgroundColor(Color.rgb(0xF5, 0xF5, 0xF5));
62 }
63
64 ImageView iv21 = new ImageView(this);
65 iv21.setImageResource(homeIcon);
66 iv21.setScaleType(ScaleType.CENTER_INSIDE);
67 TableRow.LayoutParams trp21 = new TableRow.LayoutParams();
68 trp21.width = PENNANT;
69 trp21.height = PENNANT;
70 trp21.gravity = Gravity.CENTER;
71 iv21.setLayoutParams(trp21);
72 tr20.addView(iv21);
73
74 TextView tv22 = new TextView(this);
75 TableRow.LayoutParams trp22 = new TableRow.LayoutParams();
76 trp22.width = TableRow.LayoutParams.WRAP_CONTENT;
77 trp22.height = TableRow.LayoutParams.WRAP_CONTENT;
78 trp22.gravity = Gravity.CENTER;
79 tv22.setLayoutParams(trp22);
80 tv22.setTextSize(20);
81 tv22.setText(finalResult);
82 tv22.setTextColor(Color.rgb(0x22, 0x22, 0x22));
83 tv22.setTypeface(Typeface.DEFAULT_BOLD);
84 tr20.addView(tv22);
85
86 ImageView iv23 = new ImageView(this);
87 iv23.setImageResource(guestIcon);
88 iv23.setScaleType(ScaleType.CENTER_INSIDE);
89 TableRow.LayoutParams trp23 = new TableRow.LayoutParams();
90 trp23.width = PENNANT;
91 trp23.height = PENNANT;
92 trp23.gravity = Gravity.CENTER;
93 iv23.setLayoutParams(trp23);
94 tr20.addView(iv23);
95
96 tl.addView(tr20);
97 }

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

31.03.2010, 09:42:13 via Website

gibts eine möglichkeit die row.xml zu laden, mittels getViewById die einzelnen Komponenten zu verändern (hauptsächlich getText), und dann dem layout hinzuzufügen?

Schau mal in den Listen-Source, den du letzte Tage selber hier geposted hast, da werden auch .xml dynamisch geladen und per setText() befüllt ;)

Antworten