dimanche 3 mai 2015

Android way to add new xml layout

I have an activity with a layout. After a GET request to a server, I want to dynamically add new elements to that layout.

I want to add those elements multiple times, using a for-structure.

The elements I want to add are the following:

<RelativeLayout  xmlns:android="http://ift.tt/nIICcg"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@drawable/outer_border"
            android:padding="2dp" 
            android:layout_marginTop="20dp">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@color/orange"
                android:height="40dp"
                android:paddingLeft="5dp"
                android:paddingTop="5dp"
                android:text="TW"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#ffffff"
                android:textSize="70px"
                android:width="60dp" />

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/textView3"
                android:layout_toLeftOf="@+id/checkBox1"
                android:text="inca 6 zile"
                android:textAppearance="?android:attr/textAppearanceSmall" />

        </RelativeLayout>

I've tried this:

for(int i = 0; i < homeworkList.size(); i++){
    LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);

    RelativeLayout newLayout = new RelativeLayout(currentActivity, null, R.style.HomeworkLayout);

    TextView text = new TextView(currentActivity);
    TextView text1 = new TextView(currentActivity);
    text1.setText("da");
    text.setText("nu");

    newLayout.addView(text1);
    newLayout.addView(text);

    linearLayout.addView(newLayout, relativeParams);
    }

But no result, those textview were added but on top of each other, and the relative layout I just added in that for doesn't have any of the style I added using R.style.HomeworkLayout.

What is the best way to add the elements with so much styling? Why isn't this working?

Aucun commentaire:

Enregistrer un commentaire