2012-11-23 2 views
0

linearLayout의 여러 인스턴스를 만들려고합니다. 레이아웃 파일을 만들었습니다 (아래). 자체 레이아웃 파일 및 다른 파일 (main_activity.xml)에서 배치했습니다. 스크롤 뷰 내에서 선형 레이아웃 인스턴스의 동적 번호를 추가하고 싶습니다. 내 자바 클래스에서 대규모 레이아웃을 생성프로그래밍 방식으로 여러 LinearLayouts 만들기

  1. (두 가지 이유에 대한) 내 코드가 지저분 해 보이게하기 시작 내가 프로그래밍 방식으로 그것을 통해 내 자바 코드 및 루프에서 동일한있는 LinearLayout을 만들 수 있다는 것을 알고 있지만, 차라리없는 것 :).
  2. XML에서 내 레이아웃과 Java에서 내 로직을 갖는 패러다임을 좋아합니다.

제 논리를 Java로 유지 한 다음 선형 레이아웃의 여러 "인스턴스"를 동적으로 작성/인스턴스화 할 수 있습니까? 내 생각 엔 간단한 해결책이 있지만 초보자 상태가 그것을 해결할 수있는 능력을 흐리게하는 것입니다. 웬일인지 내 Java 코드는 LinearLayout을 찾을 수 있지만 첫 번째 TextView (@ + id/submittedBy)를 찾을 수 없습니다.

는 크레이그

자바 로직을, 대단히 감사합니다

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

관련 문제