Wie füge ich einer Ansicht eine Regel hinzu, die sich bereits in meinem Layout befindet?
Mein XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylifeinformationsharedsocial.SexAcivity" >
<ListView
android:id="@+id/list_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
ich will hinzufügen layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
Wenn ich jedoch die Ansicht erneut zum Layout hinzufüge, gibt es eine IllegalStateException (offensichtlich Haha).
Wie mache ich das?
BEARBEITEN
Java:
listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);
layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);
Es wirft die Ausnahme auf die addView()
Methode
Antworten:
2 für die Antwort № 1Sie können die Ansicht nicht hinzufügen, da sie bereits hinzugefügt wurde. Sie müssen also die korrekten Layoutparameter einstellen. Sie können entweder neue erstellen oder die aktuellen abrufen und ändern. Auschecken dieser Beitrag.
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);
1 für die Antwort № 2
So wie Matthias vorschlug:
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);