Ich versuche, eine Regel unter einem TextView unter einem anderen hinzuzufügen, aber ich kann es nicht schaffen, es programmgesteuert zu tun. Ich habe eine XML wie folgt aus: Layout Params unten funktioniert nicht programmgesteuert
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#00bcd4">
<RelativeLayout
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">
<TextView
android:id="@+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category Name"
style="@style/Base.TextAppearance.AppCompat.Title"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Vel voluptatem soluta ipsa. Voluptatem est quod non explicabo aut quisquam quas. Voluptatem aliquam iure voluptas"
android:id="@+id/textView"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_below="@+id/info_text"
android:layout_above="@+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Und das funktioniert gut! Ich habe genau das Ergebnis erhalten, das ich möchte: ein CardView mit einem Kategorienamen, einer Beschreibung und 2 Knöpfen.
Also habe ich versucht, diese XML in Java zu transponieren, weil ich eine Menge Kategorien zeigen muss. Ich tat wie folgt aus:
LinearLayout linearLayoutCategory = (LinearLayout) findViewById(R.id.linearLayoutCategory);
for (int i = 0; i < categories.size(); i++) {
Category category = categories.get(i);
CardView cardView = new CardView(this);
cardView.setCardBackgroundColor(Color.parseColor("#00bcd4"));
cardView.setRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics()), Gravity.CENTER);
layoutParams.setMargins(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()), 0, 0);
cardView.setLayoutParams(layoutParams);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams1);
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams2.addRule(RelativeLayout.CENTER_HORIZONTAL);
textView.setLayoutParams(layoutParams2);
textView.setText(category.getName());
textView.setTextAppearance(this, android.R.style.TextAppearance_Large);
textView.setId(i);
relativeLayout.addView(textView);
TextView textView1 = new TextView(this);
RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams3.addRule(RelativeLayout.BELOW, textView.getId());
textView1.setLayoutParams(layoutParams3);
textView1.setText(category.getDescription());
relativeLayout.addView(textView1);
Button button = new Button(this);
relativeLayout.addView(button);
Button button1 = new Button(this);
relativeLayout.addView(button1);
cardView.addView(relativeLayout);
linearLayoutCategory.addView(cardView);
}
Das schafft eine Menge CardView mit einem guten positionierter Kategorienamen (Textview), aber ich kann nicht verwalten die Kategoriebeschreibung zu positionieren. Diese Zeile sieht nicht auf die textView1 keine Auswirkungen haben (entsprechend der Kategorie Beschreibung):
layoutParams3.addRule(RelativeLayout.BELOW, textView.getId());
Ich verstehe nicht, weil der Name der Kategorie gut positioniert ist und ich die gleiche Technik: addrule.
Habe ich etwas vergessen oder einen Fehler gemacht?
Hier ist eine picture, die Ihnen zwei Ergebnis
Vielen Dank im Voraus.