按钮的布局问题

时间:2013-12-17 17:02:52

标签: android button layout tablelayout

我正在尝试以编程方式添加表行(请参阅下面的代码)

每行包含一些文本字段并终止,有时还有一个Button

但按钮没有像文本字段那样对齐..(见输出图像)

我想为textfield和Button

设置相同的高度

我该怎么做?

非常感谢您的支持

Jcarlier

布鲁塞尔

enter image description here

公共类MainActivity扩展了活动{ ............

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

    // heading 
    TableRow tr_head = new TableRow(this);
    TextView label_club= new TextView(this);
    label_club.setText("REMARQUE");
    label_club.setGravity(Gravity.CENTER);
    label_club.setTextColor(getResources().getColor(R.color.OrangeRed));
    label_club.setBackground(getResources().getDrawable(R.drawable.border));
    tr_head.addView(label_club);
    //  etc..

    // iterate sqlite and show in table TableLayout
    Integer icount=0;
    Integer j =c.getColumnCount()-1;
    if (c != null && c.getCount() > 0){
        if (c.moveToFirst()) {
            do {
                TableRow tr = new TableRow(this);
                tr.setBaselineAligned(false);
                //tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                for( int i=0;i<j+1;i++)
                {
                    if ( (i == j) & (  c.getString(i).length() > 4) ) {
                    //map.put(icount, c.getString(i).toString());

                    Button btn=new Button(this);
                    btn.setId(icount);
                    //btn.setMaxWidth(10);
                    btn.setVisibility(View.VISIBLE);
                    btn.setText("Tableaux");
                    btn.setClickable(true);
                    //btn.setHeight(20);
                    btn.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // blabla
                                }
                            }
                        });


                    //btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    //btn.setLayoutParams (new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    //btn.setGravity(Gravity.CENTER_HORIZONTAL);
                    // btn.setBackground(getResources().getDrawable(R.drawable.border));

                    tr.addView(btn);                              }

                    else                                          {
                    TextView text=new TextView(this);
                    switch(c.getType(i))
                    {
                        case  Cursor.FIELD_TYPE_INTEGER:
                            text.setText(String.valueOf(c.getInt(i)));
                            break;
                        case  Cursor.FIELD_TYPE_STRING:
                            text.setText(c.getString(i));
                            break;

                    }

                        text.setPadding(0,0,10,0);
                        //text.setHeight(20);
                        //text.setBackground(getResources().getDrawable(R.drawable.border));
                        tr.addView(text);
                                                                }
               }

                        // add row to table
                        tl.addView(tr);
                        icount++;
              } while (c.moveToNext());
                             }
                                       }
}

 <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:id="@+id/layout"
     android:scrollbars="horizontal|vertical"
     android:layout_alignParentBottom="true"
     android:layout_height="fill_parent"

>
<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">



        <TableLayout
            android:id="@+id/table"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingTop="2dp"
            android:stretchColumns="*">
        </TableLayout>


  </HorizontalScrollView>


          </ScrollView>

0 个答案:

没有答案