Android:以编程方式在表中创建两列

时间:2011-12-05 23:48:03

标签: android android-layout

showtimeTable = new TableLayout(this);
         showtimeTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

         for(int i=0;i<object.dateList.size();i++){
              /* Create a new row to be added. */
              TableRow tr = new TableRow(this);
              tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                   /* Create testview to be the row-content. */
              mytext = new TextView(this);
             mytext.setText(object.dateList.get(i).getTextDate());
             mytext.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
             mytext.setTextAppearance(this, R.style.detaileventDate);
                   tr.addView(mytext);



                   time = new TextView(this);
                   time.setText(object.dateList.get(i).getTime());
                   time.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                   time.setTextAppearance(this, R.style.detaileventTime);
                    tr.addView(time);
         /* Add row to TableLayout. */
         showtimeTable.addView(tr,new TableLayout.LayoutParams(
                   LayoutParams.FILL_PARENT,
                   LayoutParams.WRAP_CONTENT));
         }


         layout.addView(showtimeTable);

我有这个表的所有设置,但两个文本域互相拥抱。我想要实现的是两列的外观,并在右侧有时间文本字段。

我尝试更改各自样式中的边距但不影响它。

3 个答案:

答案 0 :(得分:1)

我认为你应该在这里使用填充。它将在表格行中填充文本视图。我试图用你的代码做,只是添加了一些测试行:

mytext.setPadding(20, 3, 20, 3);
time.setPadding(20, 3, 20, 3);

工作得很好。此外,您可以使用“重力”来达到目的。请参阅此处的示例:http://developer.android.com/resources/tutorials/views/hello-tablelayout.html

答案 1 :(得分:0)

表格布局没有列的概念,但您可以通过定义这些视图的不同宽度在表格行中添加多个视图。

以下代码将根据指定的colSpan值指定列的大小。

int COL_COUNT=2;
int mScreenWidthInDp = getScreenWidthInDip(activityContext);

private View getColumnItem(String colText, int colSpan) {

        View view = mActivity.getLayoutInflater().inflate(R.layout.table_column_view, null);
        TextView textView = (TextView) view.findViewById(R.id.tablet_title);



        textView.setText(colText);
        int columnWidth = (mScreenWidthInDp / COL_COUNT) * span;
        TableRow.LayoutParams params = new TableRow.LayoutParams(columnWidth, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        params.span = colSpan;
        view.setLayoutParams(params);
        return view;
    }


    public static int getScreenWidthInDip(Activity context) {
        if (mScreenWidthInDp == 0) {
            WindowManager wm = context.getWindowManager();
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            int screenWidth_in_pixel = dm.widthPixels;
            float screenWidth_in_dip = screenWidth_in_pixel / dm.density;
            mScreenWidthInDp = (int) screenWidth_in_dip;
        }
        return mScreenWidthInDp;
    }

答案 2 :(得分:0)

您可以为表格行设置布局权重参数。这将相应地安排儿童。 mytext.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT,1f));

最后一个参数是布局权重