2013-02-09 68 views
1

嗨我想爲我的線性佈局添加2個不同的文本視圖,但不知何故,當我嘗試添加他們兩個只有第一個出現爲什麼是這種情況?這裏是我的代碼:如何在線性佈局中添加多個不同的文本視圖

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 

    // create the text view for the main string to 
    // be displayed 
    TextView displayMainText = new TextView(this); 
    displayMainText.setTextSize(15); 
    displayMainText.setText(mainString); 
    displayMainText.setLayoutParams(layoutParams); 
    displayMainText.setPadding(0, 20, 0, 0); 

    // Create the text view for the optional string to 
    // be displayed 
    TextView displayOppText = new TextView(this); 
    displayOppText.setTextSize(15); 
    displayOppText.setText(optionalString); 
    displayOppText.setLayoutParams(layoutParams); 
    displayOppText.setPadding(0, 20, 0, 0); 

    // add text views to the layout 
    LinearLayout studyTLayout = (LinearLayout) findViewById(R.id.study_time_layout); 
    studyTLayout.addView(displayMainText); 
    studyTLayout.addView(displayOppText); 
    setContentView(studyTLayout); 

我的代碼只添加第一個文本視圖中正確

回答

4

好像你沒有設置LinearLayout以垂直的方向。

studyTLayout.setOrientation(LinearLayout.VERTICAL); 
+0

o男:(謝謝你這麼多 – James 2013-02-10 00:00:29

+0

沒問題:) .. – Ahmad 2013-02-10 00:00:52

相關問題