I have written is code below, but button isn't shown bec开发者_如何学Pythonause i've set FILL_PARENT in bar layoutParametrs. How i could fixed it without using XML?
TableRow myLayout = new TableRow (this);
myLayout.setAlwaysDrawnWithCacheEnabled(true);
TextView text = new TextView (this);
text.setText("Test: ");
SeekBar bar = new SeekBar (this);
ToggleButton button = new ToggleButton (this);
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
If FILL_PARENT set for bar
If isn't
both variant is wrong
When I need to fill a row with multiple items, I typically use layout_weight and a width setting of fill_parent. That distributes the full width of the row by the proportions for which you set the weight values.
Since you're doing this in code and not xml, look at the constructor for LayoutParams that takes a third parameter. This corresponds to layout_weight:
LayoutParams - int, int, float
Try using WRAP_CONTENT and a weight parameter like this.
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,10));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,85));
精彩评论