2012-11-23 85 views
0

我正在嘗試創建一個linearLayout的多個實例。我創建了我的佈局文件(下圖)。我把它放在它自己的佈局文件和另一個文件(main_activity.xml)中,我想在滾動視圖中添加一個動態數量的線性佈局實例。我知道我可以編程方式通過它建立在我的Java代碼迴路相同的LinearLayout,但我寧可不要(主要有兩個原因)以編程方式創建多個LinearLayouts

  1. 在我的Java類創建大規模的佈局已經開始讓我的代碼顯得凌亂:)。
  2. 我喜歡在XML中使用我的佈局和在Java中使用我的邏輯的範例。

是否有可能讓我保持我的邏輯在Java中,然後動態創建/實例化我的線性佈局的多個「實例」。我的猜測是,有一個簡單的解決方案,但我的新手地位讓我不知所措。出於某種原因,我的java代碼可以找到LinearLayout,但無法找到第一個TextView(@ + id/submittedBy)。

非常感謝你, 克雷格

Java邏輯的

ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView); 
    //DEBUGGING LOOP- REMOVE ME 
    for(int i=0;i<30;i++){ 
     LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout); 

     TextView submittedByTextView = (TextView) findViewById(R.id.submittedBy); 
     submittedByTextView.setText("Craig " +i); 

     TextView submittedDate = (TextView) findViewById(R.id.dateField); 
     submittedDate.setText("SubmittedDate " +i); 

     RatingBar showRating = (RatingBar) findViewById(R.id.userShowRating); 
     showRating.setRating(3); 

     RatingBar soundRating = (RatingBar) findViewById(R.id.userSoundRating); 
     soundRating.setRating(1); 

     EditText commentText = (EditText) findViewById(R.id.userCommentEditText); 
     commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too"); 

     sv.addView(ll); 
    } 
    sv.refreshDrawableState(); 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/linear_layout_border" 
android:orientation="vertical" 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:paddingBottom="4dp" 
    android:paddingLeft="4dp" 
    android:paddingRight="4dp" 
    android:paddingTop="4dp" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

    <TextView 
     android:id="@+id/submittedBy" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:gravity="center_horizontal" 
     android:inputType="none" 
     android:paddingBottom="1dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="10dp" 
     android:paddingTop="1dp" 
     android:text="NameGoesHere" 
     android:textColor="#E6E6E6" 
     android:textSize="8sp" /> 

    <TextView 
     android:id="@+id/dateField" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/submittedBy" 
     android:ems="10" 
     android:paddingBottom="1dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="10dp" 
     android:paddingTop="1dp" 
     android:text="DateGoesHere" 
     android:textColor="#E6E6E6" 
     android:textSize="8sp" /> 
</RelativeLayout> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:paddingBottom="4dp" 
    android:paddingLeft="4dp" 
    android:paddingRight="4dp" 
    android:paddingTop="4dp" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

    <TextView 
     android:id="@+id/userShowRatingTextView" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:gravity="center_horizontal" 
     android:inputType="none" 
     android:paddingBottom="1dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="10dp" 
     android:paddingTop="1dp" 
     android:text="Show Rating" 
     android:textColor="#E6E6E6" 
     android:textSize="8sp" /> 

    <RatingBar 
     android:id="@+id/userShowRating" 
     style="?android:attr/ratingBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/userShowRatingTextView" /> 
</RelativeLayout> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:paddingBottom="4dp" 
    android:paddingLeft="4dp" 
    android:paddingRight="4dp" 
    android:paddingTop="4dp" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

    <TextView 
     android:id="@+id/userSoundRatingTextView" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toRightOf="@id/userShowRating" 
     android:gravity="center_horizontal" 
     android:inputType="none" 
     android:paddingBottom="1dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="10dp" 
     android:paddingTop="1dp" 
     android:text="Sound Rating" 
     android:textColor="#E6E6E6" 
     android:textSize="8sp" /> 

    <RatingBar 
     android:id="@+id/userSoundRating" 
     style="?android:attr/ratingBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/userSoundRatingTextView" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <EditText 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/editText2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#000000" 
     android:inputType="none" 
     android:paddingBottom="4dp" 
     android:paddingLeft="4dp" 
     android:paddingRight="4dp" 
     android:paddingTop="4dp" 
     android:text="Comment Goes Here" 
     android:textColor="#E6E6E6" 
     android:textSize="12sp" 
     android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > 

     <requestFocus /> 
    </EditText> 
</RelativeLayout> 

回答

0

你就不會遇到任何異常?我指的是循環中的最後一行,在每次迭代中將LinearLayout添加到ScrollView。你可能會試圖誇大你的佈局文件,然後將其添加到ScrollView象下面這樣:

ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView); 
// I assume this LinearLayout is the only child of the ScrollView above 
LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout); 
for(int i=0;i<30;i++){   
    // R.layout.the_layout_file is your layout from the question 
    View inflatedView = getLayoutInflater().inflate(R.layout.the_layout_file, ll, false); 
    TextView submittedByTextView = (TextView) inflatedView.findViewById(R.id.submittedBy); 
    submittedByTextView.setText("Craig " +i); 

    TextView submittedDate = (TextView) inflatedView.findViewById(R.id.dateField); 
    submittedDate.setText("SubmittedDate " +i); 

    RatingBar showRating = (RatingBar) inflatedView.findViewById(R.id.userShowRating); 
    showRating.setRating(3); 

    RatingBar soundRating = (RatingBar) inflatedView.findViewById(R.id.userSoundRating); 
    soundRating.setRating(1); 

    EditText commentText = (EditText) inflatedView.findViewById(R.id.userCommentEditText); 
    commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too"); 

    ll.addView(inflatedView); 
} 

而且,記住,你加入了很多意見,東西以避免對光滑界面。

+0

你釘了它!我並沒有理解膨脹功能的作用。非常感謝你。我同意這是很多觀點,我也很關注。我仍然在學習如何最大限度地減少需要的視圖,並保持強大的用戶界面。 再次感謝。 – mornindew

相關問題