动态TableLayout中的对齐设置

时间:2014-05-13 07:16:01

标签: android android-tablelayout tablerow

我想在用户按下添加按钮时添加x行,并且在其中一行中用户输入no类型,即在edittext中,然后根据我需要填充下面两列中的那么多行数。缺少这些列对齐..

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add requirefields" />

    <ScrollView
        android:id="@+id/scrolllayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="10dp" >

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

</RelativeLayout>

活性: -

public class MainActivity extends Activity {

    Button addBtn;
    TableLayout mTable;
    TableLayout timingsTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addBtn = (Button) findViewById(R.id.button1);
        mTable = (TableLayout) findViewById(R.id.tablelayout);

        addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "btnonclicklistener",
                        Toast.LENGTH_LONG).show();
                createTable();

            }
        });
    }

    public void createTable() {
        // mTable.setColumnShrinkable(1, true);
        mTable.setStretchAllColumns(true);
        mTable.setShrinkAllColumns(true);

        TableRow rowMName = new TableRow(MainActivity.this);

        TableRow rowDescription = new TableRow(MainActivity.this);
        TableRow rowType = new TableRow(MainActivity.this);
        TableRow rowTimesADay = new TableRow(MainActivity.this);
        TableRow rowTimings = new TableRow(MainActivity.this);
        TableRow rowNoofTimings = new TableRow(MainActivity.this);

        TextView mNametv = new TextView(MainActivity.this);
        mNametv.setText("MNAME");
        TextView descriptiontv = new TextView(MainActivity.this);
        descriptiontv.setText("Description");
        TextView typetv = new TextView(MainActivity.this);
        typetv.setText("TYPE");
        TextView timesAdaytv = new TextView(MainActivity.this);
        timesAdaytv.setText("TimesADay");
        TextView timingstv = new TextView(MainActivity.this);
        timingstv.setText("Timings");

        rowMName.addView(mNametv);
        rowDescription.addView(descriptiontv);
        rowType.addView(typetv);
        rowTimesADay.addView(timesAdaytv);
        rowTimings.addView(timingstv);

        EditText etMName = new EditText(MainActivity.this);
        EditText etDescription = new EditText(MainActivity.this);
        EditText etType = new EditText(MainActivity.this);
        final EditText etTimesAday = new EditText(MainActivity.this);
        // EditText etTimings = new EditText(MainActivity.this);
        timingsTable = new TableLayout(MainActivity.this);
        TextView dosagetv = new TextView(MainActivity.this);
        dosagetv.setText("Dosage");

        rowMName.addView(etMName);
        rowDescription.addView(etDescription);
        rowType.addView(etType);
        rowTimesADay.addView(etTimesAday);
        rowNoofTimings.addView(timingsTable);
        rowTimings.addView(dosagetv);

        etTimesAday.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                if (!s.toString().equals("")) {
                    int length = Integer.parseInt(s.toString());
                    if (length >= 1) {
                        timingsTable.removeAllViews();
                        timingsTable.setVisibility(View.VISIBLE);
                        dynamicEditBox(etTimesAday.getText().toString());
                        Toast.makeText(getBaseContext(), "if condition",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getBaseContext(), "else condition",
                            Toast.LENGTH_SHORT).show();
                    timingsTable.setVisibility(View.GONE);
                    timingsTable.removeAllViews();
                    if (s.equals("")) {
                        etTimesAday.setText("");
                    }
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
        mTable.addView(rowMName);
        mTable.addView(rowDescription);
        mTable.addView(rowType);
        mTable.addView(rowTimesADay);
        mTable.addView(rowTimings);
        mTable.addView(rowNoofTimings);

    }

    public void dynamicEditBox(String id) {
        timingsTable.setStretchAllColumns(true);
        timingsTable.setShrinkAllColumns(true);

        for (int i = 0; i < Integer.parseInt(id); i++) {

            TableRow tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            TableRow.LayoutParams parms = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            TextView ed = new TextView(getApplication());
            ed.setInputType(InputType.TYPE_CLASS_NUMBER);
            ed.setLayoutParams(parms);
            ed.setId(i);
            ed.setHint("enter number");
            ed.setBackgroundColor(Color.DKGRAY);
            tr.addView(ed);

            EditText b = new EditText(getApplication());
            b.setLayoutParams(new TableRow.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            b.setId(i);
            b.setBackgroundColor(Color.LTGRAY);
            b.setText("dosage");
            tr.addView(b);

            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 10, 10, 0);

            timingsTable.setLayoutParams(layoutParams);
            timingsTable.addView(tr);

        }
    }

}

目前我的情况如下......

enter image description here

我需要相应地调整这些时间和剂量列。帮助我设置这个对齐方式。

1 个答案:

答案 0 :(得分:4)

在dynamicEditBox方法中进行以下更改后,我得到了对齐

public void dynamicEditBox(String id) {
    timingsTable.setStretchAllColumns(true);
    timingsTable.setShrinkAllColumns(true);

    for (int i = 0; i < Integer.parseInt(id); i++) {

        TableRow tr = new TableRow(MainActivity.this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.WRAP_CONTENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
        TableRow.LayoutParams parms = new TableRow.LayoutParams(0,
                TableRow.LayoutParams.WRAP_CONTENT, 1f);
        parms.setMargins(5, 5, 5, 5);
        TextView tv = new TextView(getApplication());

        tv.setLayoutParams(parms);
        tv.setWidth(50);
        tv.setPadding(20, 5, 5, 5);
        tv.setId(i);
        tv.setHint("10:25am");
        tv.setBackgroundColor(Color.LTGRAY);
        tr.addView(tv);

        EditText et = new EditText(getApplication());
        et.setLayoutParams(parms);
        et.setPadding(5, 5, 5, 5);
        et.setGravity(Gravity.CENTER);
        et.setRawInputType(Configuration.KEYBOARD_12KEY);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);

        et.setId(i);
        et.setBackgroundColor(Color.LTGRAY);
        et.setHint("dosage");
        tr.addView(et);

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(0,
                TableRow.LayoutParams.WRAP_CONTENT, 1f);
        layoutParams.setMargins(-15, 10, 0, 0);

        timingsTable.setLayoutParams(layoutParams);
        timingsTable.addView(tr);

    }
}

我得到了以下方式。

enter image description here