2012-04-17 31 views
1

我需要創建一個控件,以便在工作中更輕鬆地開發一些屏幕。
該控件非常簡單,只需兩個按鈕和一個SeekBar。這些按鈕僅用於增加或減少SeekBar值。SeekBar在TableRow中的寬度?

這裏是我的代碼:

public class SeekbarCheckbox extends TableLayout { 
private SeekBar _Seekbar; 
private CheckBox _CheckBox; 
private TableRow _tr; 

public SeekbarCheckbox(Context context, int max) { 
    super(context); 

    _tr = new TableRow(context); 

    Button bt1 = new Button(context); 
    bt1.setText("-"); 
    _tr.addView(bt1); 

    _Seekbar = new SeekBar(context); 
    _Seekbar.setMax(max); 
    _tr.addView(_Seekbar); 

    Button bt2 = new Button(context); 
    bt2.setText("+"); 
    _tr.addView(bt2); 


    this.addView(_tr, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

} 

}

的事情是,當我在應用程序中使用這個控制我得到一個無用的寬度的搜索欄。我分享一張圖片看結果。

我無法發佈圖片,所以我共享了一個鏈接。 http://img685.imageshack.us/img685/6756/controlk.png

+0

嗨可以請你讓我知道你在哪裏以及如何使用這個customlayout – Manju 2012-04-17 04:38:19

回答

1

我試圖找到你的問題的解決方案。如果在seekbar上將weight設置爲1,那麼它將獲得所有空間,接受按鈕所需的最小空間。 對我來說這段代碼工作正常。

public class SeekbarCheckbox extends TableLayout { 
    private SeekBar _Seekbar; 
    private CheckBox _CheckBox; 
    private TableRow _tr; 

    public SeekbarCheckbox(Context context, int max) { 
     super(context); 

     _tr = new TableRow(context); 

     Button bt1 = new Button(context); 
     bt1.setText("-"); 
     _tr.addView(bt1); 

     _Seekbar = new SeekBar(context); 
     _Seekbar.setMax(max); 
     TableRow.LayoutParams ob = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, 
                   LayoutParams.WRAP_CONTENT, 
                   1); 
     _tr.addView(_Seekbar, ob); 

     Button bt2 = new Button(context); 
     bt2.setText("+"); 
     _tr.addView(bt2); 

     _tr.setBackgroundColor(Color.BLUE); 
     this.addView(_tr); 
    } 
} 
+0

這是我更喜歡的選項... – Rothariger 2012-04-17 23:54:01

1

設置視圖的權重值應該有所斬斷。

對於按鈕:

setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2f)); 

搜索欄:

setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 10f)); 
+0

感謝您的時間! – Rothariger 2012-04-17 23:54:45

0

替換你的佈局代碼像下面

public SeekBarcheckbox(Context context, int max) { 
    super(context); 

    _tr = new TableRow(context); 
    _tr.setWeightSum(10f); 
    Button bt1 = new Button(context); 
    bt1.setText("-"); 
    TableRow.LayoutParams layoutParams=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,2f); 
    _tr.addView(bt1, layoutParams); 
    _Seekbar = new SeekBar(context); 
    _Seekbar.setMax(max); 
    TableRow.LayoutParams layoutParams1=new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,7f); 
    _tr.addView(_Seekbar,layoutParams1); 
    Button bt2 = new Button(context); 
    bt2.setText("+"); 
    LinearLayout.LayoutParams layoutParams3=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1f); 
    _tr.addView(bt2,layoutParams3); 
    LinearLayout.LayoutParams layoutParams2=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    this.addView(_tr,layoutParams2); 

} 

和我的活動中使用下面的代碼

seekBarcheckbox=new SeekBarcheckbox(this, 100); 
    setContentView(seekBarcheckbox); 

其工作對我來說...

+0

感謝您的時間! – Rothariger 2012-04-17 23:54:29

相關問題