Visualizzazione stampabile
-
Convertire XML in java
Sto cercando di convertire questi 4 elementi di un layout XML in codice java da mettere direttamente in una classe, ma non mi riesce.
Qualcuno potrebbe aiutarmi?
codice:
<TextView
android:id="@+id/text_route_section_station"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="First station"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/image_route_section_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text_route_section_station"
android:contentDescription="@string/text_route_section_line_img_title"
android:src="@drawable/tstop" />
<TextView
android:id="@+id/text_route_section_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/image_route_section_line"
android:layout_toRightOf="@+id/image_route_section_line"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:text="54 min"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/text_route_section_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/image_route_section_line"
android:layout_toRightOf="@+id/text_route_section_time"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:text="Line x"
android:textAppearance="?android:attr/textAppearanceSmall" />
-
Per ora ho scritto il codice della prima TextView (ma non so se è giusto)
Considera che sono alla mia prima esperienza con Android.
Per ora quello che ho scritto è
codice:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.routes_transfers_list);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView1.setId("text_route_section_station1");
layout.addView(textView1);
Ora dovrei scrivere il codice per i seguenti elementi, ma no so come impostare le posizioni.
-
Sono arrivato a questo codice che mi mostra tutti gli elementi, ma mi manca di riuscire ad impostare i margini.
codice:
TextView textView1 = new TextView(this);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView1.setId(1);
textView1.setText("Stazione1");
layout.addView(textView1,params1);
ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(R.drawable.tstop);
imageView1.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW,textView1.getId());
imageView1.setId(2);
layout.addView(imageView1, params2);
TextView textView2 = new TextView(this);
textView2.setId(3);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.BELOW,textView1.getId());
params3.addRule(RelativeLayout.RIGHT_OF,imageView1.getId());
textView2.setText("12:03~12:20");
layout.addView(textView2,params3);
TextView textView3 = new TextView(this);
textView3.setId(4);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.BELOW,textView1.getId());
params4.addRule(RelativeLayout.RIGHT_OF,textView2.getId());
textView3.setText("Nome Linea");
layout.addView(textView3,params4);
Devo impostare un margine di 10dp a sinistra di textView2 e 10dp a sinistra di textImage3