2012-03-01 5 views
0

複数の選択肢のフォームを生成する簡単なプログラムを作成する。私はそれぞれの質問をするためのテンプレートとして機能するsing_select.xmlを持っています。次にコードでは、カスタマイズしたこれらのテンプレートをmain.xmlに入れたいと思っていました。最初の質問にはうまくいきますが、その後の質問は表示されません。私が間違っていることを確信していない。最初の質問を手作業で隠してしまったため、重複はありません。 (無関係なUI要素を取り除か)Android Dev:XMLテンプレートから複数のレイアウトを動的に生成できない

Javaファイル

public class FormFillerActivity extends Activity 
{ 
    private LinearLayout mQuestionList; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //Must come before setContentView or program crashes 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     //Must set before accessing layout elements or program crashes 
     setContentView(R.layout.main); 

     mQuestionList = (LinearLayout) findViewById(R.id.Body_Layout); 

     initForm(); 
    } 

    private void initForm() 
    { 
     int count = 1; 

     ArrayList<String> answers = new ArrayList<String>(); 
     answers.add("Single"); 
     answers.add("Married"); 
     answers.add("Separated"); 
     answers.add("Divorced"); 
     mQuestionList.addView(addSingSelectQuestion(count++, "What is your marital status?", answers)); 

     answers.clear(); 
     answers.add("Male"); 
     answers.add("Female"); 
     mQuestionList.addView(addSingSelectQuestion(count++, "What is your gender?", answers)); 

    } 

    private View addSingSelectQuestion(int count, String question, ArrayList<String> answers) 
    { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View container = inflater.inflate(R.layout.sing_select, null); 

     ((TextView) container.findViewById(R.id.Sing_Select_Num)).setText(count + ") "); 
     ((TextView) container.findViewById(R.id.Sing_Select_Text)).setText(question); 

     RadioGroup rg = (RadioGroup) container.findViewById(R.id.Sing_Select_Answer); 

     //Generate radio group answers 
     Iterator<String> it = answers.iterator(); 
     while (it.hasNext()) 
     { 
      RadioButton rb = new RadioButton(rg.getContext()); 
      RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 
        RadioGroup.LayoutParams.WRAP_CONTENT); 
      String ans = it.next(); 

      rb.setId(answers.indexOf(ans)); 
      rb.setLayoutParams(params); 
      rb.setText(ans); 
      rb.setTextColor(getResources().getColor(R.color.black)); 
      rb.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_normal)); 
      rg.addView(rb); 
     } 

     return container; 
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
... 
    <ScrollView 
     android:id="@+id/Body_Scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/Footer" 
     android:layout_below="@id/Title" 
     android:scrollbars="vertical" > 

     <LinearLayout 
      android:id="@+id/Body_Layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/marg_normal" 
      android:padding="@dimen/pad_large" > 
     </LinearLayout> 
    </ScrollView> 
... 

sing_select.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Sing_Select_Layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" 
    android:padding="8dp" > 

    <TextView 
     android:id="@+id/Sing_Select_Num" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="#) " 
     android:textColor="@color/black" 
     android:textSize="@dimen/txt_normal" /> 

    <TextView 
     android:id="@+id/Sing_Select_Text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Sing_Select_Num" 
     android:layout_toRightOf="@+id/Sing_Select_Num" 
     android:text="The Question?" 
     android:textColor="@color/black" 
     android:textSize="@dimen/txt_normal" /> 

    <RadioGroup 
     android:id="@+id/Sing_Select_Answer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/Sing_Select_Text" 
     android:layout_below="@+id/Sing_Select_Text" 
     android:layout_toLeftOf="@+id/Sing_Select_Trans_Button" > 
    </RadioGroup> 

    <Button 
     android:id="@+id/Sing_Select_Trans_Button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/btn_big" 
     android:padding="8dp" 
     android:text="Accept" 
     android:textSize="@dimen/txt_button" /> 

</RelativeLayout> 

答えて

1

ためverticalに向きを設定するようにしてください* Body_Layout * LinearLayout。私は最初のものの後ろの次の行を画面から押し出すと思います(膨張したビューの幅は親を埋めるように設定されています)。

+0

男私はそんなにばかです。それはそれを修正した。私がそのようなものを逃したときにそれを憎む。 –

+0

@JaySoyer私の答えがあなたの問題を修正した場合は、それを正しいものとしてマークしてください。 – Luksprog

+0

申し訳ありません。ここに投稿するのが初めてです。私はそれをすることができたか分からなかった。今すぐマークする必要があります。 –

関連する問題