1

Başka bir TextView koymak için AŞAĞIDA bir kural eklemek çalışıyorum ama programlı olarak bunu başaramıyorum. Layout Params aşağıda programsal olarak çalışmıyor

 <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> 

Ve bu iyi çalışır: Böyle bir XML var! Tam olarak istediğim sonucu aldım: kategori adı, açıklaması ve 2 düğmeli bir CardView.
Bu yüzden bu XML'i Java'ya aktarmaya çalıştım çünkü göstereceğim çok fazla kategorim var. Böyle yaptı:

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); 
    } 

Bu iyi konumlandırılmış kategori adıyla (TextView'un) ile CardView bir sürü oluşturur ama kategori açıklamasını konumlandırmak için yönetemez.

 layoutParams3.addRule(RelativeLayout.BELOW, textView.getId()); 

kategorisi adı konumlandırılmış iyi ve ben aynı tekniği kullanılan çünkü anlamıyorum: addRule Bu hat textView1 üzerinde herhangi bir etkisi (kategori açıklaması karşılık gelir) sahip görünmüyor.
Bir şey mi unuttum veya hata yaptım mı?
İşte size bir iki puan göstermek için bir picture olduğunu
Şimdiden teşekkür ederiz.

cevap

2

sorun kural TextView'un kimliği temel olmasıdır:

layoutParams3.addRule(RelativeLayout.BELOW, textView.getId()); 

Ama oluşturmak beri sizin TextView kodundan, kendisine atanmış herhangi kimliği yok. XML'de, TextView, android:id="@+id/info_text" kimliğine sahiptir, bu nedenle XML'de iyi çalışır.

XML düzeninizi kod haline dönüştürmenize gerek yoktur - neye ihtiyacınız var - her bir kategori için düzeninizi şişirmeniz ve sonuçlara kabı eklemeniz yeterlidir (dikey LinearLayout'u kategorileriniz için bir kap olarak kullandığınıza inanıyorum) :

main_layout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/linearLayoutCategory" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

category.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.CardView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     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> 

iş mantığı:

LinearLayout linearLayoutCategory = (LinearLayout)findViewById(R.id.linearLayoutCategory); 
for (int i = 0; i < categories.size(); i++) { 
    View category = getLayoutInflater().inflate(R.layout.category, linearLayoutCategory, false); 

    TextView infoText = category.findViewById(R.id.info_text); 
    infoText.setText(/*category name?*/); 

    linearLayoutCategory.addView(category); 
}