2013-05-09 51 views
0

我在android中動態創建錶行並在該行創建按鈕。所有的渲染都很好,但創建的按鈕填充父項,即佔用父行的全部寬度。我希望它是一個固定寬度的數組,即150px。動態創建的按鈕不填充父項

我的代碼

TableRow rows = new TableRow(this); 
TableLayout.LayoutParams tableRowParamss= 
    new TableLayout.LayoutParams 
    (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT); 

    rows.setBackgroundColor(Color.parseColor("#62b0ff")); 
    tableRowParamss.setMargins(0, 0, 0, 40); 
    rows.setLayoutParams(tableRowParamss); 

    Button ib = new Button(this); 
    //ib.setBackgroundResource(R.drawable.accept); 
    final float scale = getResources().getDisplayMetrics().density; 
    int heightDp = (int) (33 * scale + 0.5f); 
    int widthDp = (int) (60 * scale + 0.5f); 
    ViewGroup.LayoutParams bLp = new ViewGroup.LayoutParams(widthDp,heightDp); 

    rows.addView(ib); 

    table.addView(rows); 

我怎樣才能使按鈕的寬度固定爲150px和對齊排的右側?

+0

設置按鈕的佈局PARAMS具有150 – 2013-05-09 12:00:24

回答

0
 Import for TableRowParam 
     // import android.widget.TableRow.LayoutParams;  


     @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.text); 

      TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 

    TableRow rows = new TableRow(this); 
    TableLayout.LayoutParams tableRowParamss= 
      new TableLayout.LayoutParams 
       (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT); 

    rows.setBackgroundColor(Color.parseColor("#62b0ff")); 
    tableRowParamss.setMargins(0, 0, 0, 40); 
    rows.setLayoutParams(tableRowParamss); 

    Button ib = new Button(this); 
    //ib.setBackgroundResource(R.drawable.accept); 
      // import android.widget.TableRow.LayoutParams; 

    LayoutParams rowParam = new LayoutParams(150, LayoutParams.WRAP_CONTENT); 
    ib.setLayoutParams(rowParam); 

    rows.addView(ib); 

    table.addView(rows); 


} 



<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

    </TableLayout> 

</LinearLayout> 
+0

你好,使用xml使用TableLayout,然後在onCreate中膨脹並動態添加tableRow。我測試這個代碼,它對我很好。 – 2013-05-09 12:41:27

+0

嗨我試過這個..但我的按鈕填寫父行...任何想法? – ramesh 2013-05-10 03:45:46

+0

你可以給我你的XML佈局,其中你動態添加表格行 – 2013-05-10 03:47:15

0

嘗試此爲行中的固定尺寸的按鈕 -

TableRow.LayoutParams bLp = new TableRow.LayoutParams(); 
    // Set the height and width as per your requirement 
    bLp.width=100; 
    bLp.height=50;  

    rows.addView(ib,bLp); 
+0

寬度喜我嘗試這樣做..但仍然我的按鈕填寫父行...任何想法? – ramesh 2013-05-10 03:45:23

+1

解決了嗎? – Anukool 2013-05-10 04:48:47

+0

我在該表格行內創建了一個線性佈局。這就是我解決問題的方法。順便說一下,有什麼選擇刷新恢復活動(意味着退出後重新打開)? – ramesh 2013-05-10 04:53:54

0
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams(); 
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of); 
    params.width = 150; 
    params.height = 100; 
    button.setLayoutParams(params); 
0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
button.setLayoutParams(params);