动态创建的按钮不会填充父级

时间:2013-05-09 11:29:20

标签: java android android-layout

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分: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>

答案 3 :(得分:0)

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);