2014-09-25 50 views
0

我正在嘗試膨脹佈局內的佈局,並在其中添加幾個TextViews。要做到這一點,我有一個我想要爲每個textview標題的arrayList,並且我還有一個與arrayList大小相同的TextViews數組。事情是,這段代碼只會膨脹一個TextView(arrayList中的最後一個)。在RelativeLayout中的anohter TextView的右側的TextViews不能正常工作Android

public void inflatePeriodGradeRightOf() { 
    AcademicStatusController.getInstance(this); 
    Log.d(TAG, "type="+instanceType+" id= "+instanceId); 
    ArrayList<Item> periodsInfo = AcademicStatusController 
      .getPeriodsInfo(instanceType, instanceId); 

    RelativeLayout llDisplayData = (RelativeLayout) findViewById(R.id.help_relative_layout_for_periods); 

    // Params to add some new rules 
    RelativeLayout.LayoutParams relativeLayoutParams; 
    relativeLayoutParams = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 
    TextView tvtxt; 
    TextView[] textViews = new TextView[periodsInfo.size()]; 
    int i = 0; 

    for (Item info : periodsInfo) { 
     // Inflate the view of the period 
     View customView = inflater.inflate(
       R.layout.activity_academic_status_period_layout, null); 
     textViews[i] = new TextView(this); 

     // Add the first textView and then add the others right of the last 
     // one 
     PeriodItem item = (PeriodItem) info; 
     if (periodsInfo.get(0) == info) { 
      // Set the text to the TextView. 
      textViews[i].setText(item.getName()); 
      textViews[i].setId(i+1); 
      llDisplayData.addView(customView); 
     } else { 
      // Set the text to the TextView. 
      textViews[i].setText(item.getName()) 
      textViews[i].setId(i+1); 
      relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textViews[i-1].getId()); 
       textViews[i].setLayoutParams(relativeLayoutParams); 
      llDisplayData.addView(customView, relativeLayoutParams); 
     } 

     i++; 
    } 
} 

我的「主」佈局這就是我應該添加TextViews看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/carla123456" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <ImageView 
      android:id="@+id/academic_status_banner" 
      android:layout_width="wrap_content" 
      android:layout_height="200dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/banner_team" /> 

     <TextView 
      android:id="@+id/academic_status_average" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="50dp" 
      android:layout_marginTop="20dp" 
      android:background="@drawable/rounded_corner_blue" 
      android:paddingBottom="5dp" 
      android:paddingLeft="20dp" 
      android:paddingRight="20dp" 
      android:paddingTop="5dp" 
      android:text="Promedio: ND" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#FFF" /> 

     <TextView 
      android:id="@+id/academic_status_subjects" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/academic_status_banner" 
      android:background="@color/akdemia_blue" 
      android:gravity="center" 
      android:paddingBottom="5dp" 
      android:paddingTop="5dp" 
      android:text="Materias" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#FFF" /> 

     <ListView 
      android:id="@+id/academic_status_subjects_list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/academic_status_subjects" > 
     </ListView> 

     <RelativeLayout 
      android:id="@+id/help_relative_layout_for_periods" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="100dp" 
      android:layout_centerHorizontal="true" > 

     </RelativeLayout> 
    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- The navigation drawer --> 
    <!-- should not be larger than 320 to show content --> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#111" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 

</android.support.v4.widget.DrawerLayout> 

什麼我想實現的是這個樣子:

enter image description here

但我只能得到它膨脹arrayList中的最後一個元素。

希望任何人都可以幫助我這個,在此先感謝。

+0

這是因爲ID始終相同,因此您將textviews全部添加到其他頂部。這就是爲什麼你只看到最後一個,因爲它留在所有其他人之上。 – 2014-09-25 16:22:34

+0

我剛剛意識到這一點,並且實際上改變了ID!讓我更新我的代碼,以便您可以看到 – 2014-09-25 16:24:28

+0

@PedroOliveira完成。我這樣做,我仍然只得到數組中的最後一個textView膨脹 – 2014-09-25 16:26:01

回答

0

我發現了這個問題。我試圖膨脹每個TextView而不是每個RelativeLayout!

我有一個視圖數組和一個TextViews數組,我膨脹了我的循環外部的第一個佈局,有一個起點。然後爲每個佈局和textViews充氣視圖並找到TextView,爲TextView設置文本(在我的情況下是periodInfo ArrayList中的句點名稱),爲View和TextView生成ids,設置參數並用這些參數添加視圖。

我犯的一個錯誤是對每個佈局使用了LayoutParams的SAME INSTANCE,這是行不通的!您需要爲要添加的每個View創建一個新的LayoutParams實例。

這裏是膨脹另一個RelativeLayout的RelativeLayout右邊的代碼。

@SuppressLint("InflateParams") 
    public void inflatePeriodGrade() { 
     AcademicStatusController.getInstance(this); 
     ArrayList<Item> periodsInfo = AcademicStatusController.getPeriodsInfo(
       instanceType, instanceId); 

     // Find RelativeLayout where the period grade will be placed 
     RelativeLayout llDisplayData = (RelativeLayout) findViewById(R.id.help_linear_layout_for_periods); 

     View[] customViews = new View[periodsInfo.size()]; 
     TextView[] textViews = new TextView[periodsInfo.size()]; 
     // 
     // Inflate the view of the period 
     customViews[0] = inflater.inflate(
       R.layout.activity_academic_status_period_layout, null); 
     textViews[0] = (TextView) customViews[0] 
       .findViewById(R.id.academic_status_grade); 

     // Set the text to the TextView. 
     textViews[0].setText(((PeriodItem) (periodsInfo.get(0))).getName()); 
     customViews[0].setId(View.generateViewId()); 
     textViews[0].setId(View.generateViewId()); 
     llDisplayData.addView(customViews[0]); 

     int i = 1; 
     for (Item item : periodsInfo) { 

      PeriodItem period = (PeriodItem) item; 

      if (periodsInfo.get(0) == period) { 
       continue; 
      } 

      customViews[i] = inflater.inflate(
        R.layout.activity_academic_status_period_layout, null); 
      textViews[i] = (TextView) customViews[i] 
        .findViewById(R.id.academic_status_grade); 
      textViews[i].setText(period.getName()); 
      RelativeLayout.LayoutParams newParams; 
      newParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      newParams.addRule(RelativeLayout.RIGHT_OF, 
        customViews[i - 1].getId()); 
      customViews[i].setId(View.generateViewId()); 
      textViews[i].setId(View.generateViewId()); 
      customViews[i].setLayoutParams(newParams); 
      llDisplayData.addView(customViews[i], newParams); 
      i++; 
     } 

    } 

有可能是一個更有效的方法來做到這一點,但這是我做到了這一點。我真的希望這段代碼可以幫助某人:)

+0

你好,你在那裏? @卡拉斯塔比爾 – Piyush 2017-08-11 11:10:12