2012-05-25 123 views
0

我正面臨着在對話框中渲染TextView和Seekbar的問題。簡單佈局問題

我是從android site

練習本教程中的問題是在for循環TextView的&搜索欄都應該加入5倍,並應顯示在對話框。但只顯示一個TextView。

下面是代碼:

public void onCheckedChanged(RadioGroup rgroup, int rbutton) { 
    String eqSettingName = ((RadioButton) findViewById(rbutton)).getText() 
      .toString(); 
    if (eqSettingName.equals("Custom")) { 
     Dialog dialog = new Dialog(this); 
     dialog.setTitle("Custom Equalizer"); 
     LinearLayout LL = new LinearLayout(this); 

     short noOfBands = mEqualizer.getNumberOfBands(); 
     final short minEQLevel = mEqualizer.getBandLevelRange()[0]; 
     final short maxEQLevel = mEqualizer.getBandLevelRange()[1]; 

     for (short i = 0; i < noOfBands; i++) { 
      short band = i; 

      TextView freqTV = new TextView(this); 
      freqTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      freqTV.setGravity(Gravity.CENTER_HORIZONTAL); 
      freqTV.setText((mEqualizer.getCenterFreq(band))/1000 + " Hz"); 
      LL.addView(freqTV); 

      SeekBar bar = new SeekBar(this); 
      bar.setLayoutParams(layoutParams); 
      bar.setMax(maxEQLevel - minEQLevel); 
      bar.setProgress(mEqualizer.getBandLevel(band)); 
      LL.addView(bar); 


     } 

     /* 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.customseekbar, 
       (ViewGroup) findViewById(R.id.rlCustomEqualizerSeekBar)); 
     */ 
     dialog.addContentView(LL, layoutParams); 
     dialog.show(); 
    } 

} 
+0

爲什麼LayoutInflater被註釋掉了?這可能是你的錯誤所在。 – Codeman

回答

1

LinearLayout默認爲orientation="horizontal"。改變它的方向爲垂直,你會看到你想要的。

LL.setOrientation(LinearLayout.VERTICAL); 
+0

謝謝你......但我對佈局很陌生。你能解釋它是如何工作的 –

+0

@rahul s:你的佈局方向是水平的,這意味着你的10個小工具中的每一個都顯示在另一個的右邊(因此不可見)。通過將方向設置爲垂直,每個小部件都顯示在前一個下方。 –

0

是您的正確的LinearLayout導向?

LL.setOrientation(LinearLayout.VERTICAL); 
+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – 2012-11-17 14:50:20

+0

這可能不是答案,但我之前遇到過這個問題,那就是答案。我討厭聽起來很滑稽,所以我不會這樣做,但是警惕你認爲不合適的答案並不是對這個話題的貢獻。 – Phix