2012-04-20 58 views
0

嘗試將SeekBar添加到對話框時,我意識到我需要TextView反映SeekBarsetProgress()。我實現它是這樣:但是RelativeLayout不適合視圖中的所有內容

private void customDialogTimeout() { 
    LinearLayout ll = new LinearLayout(getSherlockActivity()); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    RelativeLayout input = new RelativeLayout(getSherlockActivity()); 

    final SeekBar timeoutSeekBar = new SeekBar(getSherlockActivity()); 
    timeoutSeekBar.setId(1); 
    final TextView seekBarStatus = new TextView(getSherlockActivity()); 
    seekBarStatus.setId(2); 

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout.LayoutParams lay1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay1.addRule(RelativeLayout.CENTER_VERTICAL); 
    lay1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 

    RelativeLayout.LayoutParams lay2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay2.addRule(RelativeLayout.CENTER_VERTICAL); 
    lay2.addRule(RelativeLayout.RIGHT_OF, timeoutSeekBar.getId()); 

    layoutParams.setMargins(30, 20, 30, 0); 

    input.addView(timeoutSeekBar,lay1); 
    input.addView(seekBarStatus, lay2); 

    ll.addView(input, layoutParams); 

產生的觀點似乎 「推出的」 TextView

enter image description here

我在做什麼錯?如果我接近這個錯誤的方式,請讓我知道。

回答

1

SeekBar有其寬度設置爲RelativeLayout.LayoutParams.MATCH_PARENT,當你添加到其離開TextView它顯然不會演出,因爲它被推出屏幕(作爲SeekBar已經填滿整個寬度):

RelativeLayout.LayoutParams lay1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay1.addRule(RelativeLayout.CENTER_VERTICAL); 
    lay1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    lay1.addRule(RelativeLayout.LEFT_OF, seekBarStatus.getId()); 

    RelativeLayout.LayoutParams lay2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay2.addRule(RelativeLayout.CENTER_VERTICAL); 
    lay2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

    layoutParams.setMargins(30, 20, 30, 0); 
    input.addView(seekBarStatus, lay2); 
    input.addView(timeoutSeekBar,lay1);