0

我正在android中动态创建一个表格行并在该行中创建一个按钮。一切都很好,但创建的按钮填充了父级,即占据了父行的整个宽度。我希望它是一个固定宽度的数组,即 150 像素。

我的代码

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 并与行的右侧对齐?

4

4 回答 4

0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);
于 2013-05-09T12:42:45.823 回答
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);
于 2013-05-09T12:18:11.497 回答
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>
于 2013-05-09T12:40:00.060 回答
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);
于 2013-05-09T11:51:54.327 回答