以编程方式生成的表行的内容对齐问题

时间:2014-06-13 06:01:47

标签: java android alignment tablerow

我创建了一个活动,我希望以编程方式在表中生成行。该表将在每行中包含一个复选框,imageview和2个textview。行已生成,但行中内容的对齐方式不正确。

活动代码

public class PlanTripActivity extends ActionBarActivity {

    int tableRowCount = 0;
    TableLayout plan_a_trip_table;

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

        plan_a_trip_table = (TableLayout) findViewById(R.id.plan_a_trip_table);

        planTripBody("", "Test Name Type",
                "P1");
    }

    //table body
    public void planTripBody(String imagename, String nameType,
            String placeId) {

        tableRowCount = tableRowCount + 1;


        // ----------------plan_a_trip_table table
        // body------------------------------------------

        TableRow plan_trip_tr_data = new TableRow(this);
        plan_trip_tr_data.setId(10);
        //plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
        plan_trip_tr_data.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        // select checkbox
        final CheckBox select_checkbox = new CheckBox(this);
        select_checkbox.setId(20);
        select_checkbox.setTextColor(Color.BLACK);
        select_checkbox.setGravity(Gravity.CENTER);
        select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        plan_trip_tr_data.addView(select_checkbox);

        // image
        final ImageView place_imageview = new ImageView(this);
        place_imageview.setId(20);
        // test_name.setText(parser.getValue(e, KEY_SETNAME));
        //place_imageview.setPadding(5, 5, 5, 5);
        place_imageview.setImageResource(R.drawable.planatrip);
        plan_trip_tr_data.addView(place_imageview);// add the column to
                                                        // the table row
                                                        // here

        // Type
        final TextView name_type = new TextView(this);
        name_type.setId(21);// define id that must be unique
        // no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
        // the text for the header
        name_type.setText(Html.fromHtml(nameType));
        name_type.setTextColor(Color.BLACK); // set the color
        name_type.setPadding(5, 5, 5, 5); // set the padding (if
                                                // required)
        name_type.setGravity(Gravity.LEFT);
        name_type.setTextSize(18);
        plan_trip_tr_data.addView(name_type); // add the column
                                                            // to
                                                            // the table row
                                                            // here


        // ID
        final TextView place_id = new TextView(this);
        place_id.setId(21);// define id that must be unique
        // total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
        // the text for the header
        place_id.setText(placeId);
        place_id.setTextColor(Color.BLACK); // set the color
        place_id.setPadding(5, 5, 5, 5); // set the padding (if
                                                // required)
        place_id.setGravity(Gravity.CENTER);
        place_id.setTextSize(10);
        plan_trip_tr_data.addView(place_id); // add the column
                                                            // to the
        // table row here


        plan_a_trip_table.addView(plan_trip_tr_data,
                new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        // ----------------------On click name_type

        name_type.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (!select_checkbox.isChecked()) {
                    select_checkbox.setChecked(true);
                } else {
                    select_checkbox.setChecked(false);
                }

            }
        });
        // ----------------------On click customer_name



    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" >

                <TextView
                    android:id="@+id/city_name_textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="City name"
                    android:textSize="20sp" />

                <ImageView
                    android:id="@+id/update_indicator_imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@android:drawable/presence_busy" 
                    android:layout_marginLeft="10dp"/>
            </TableRow>

            <TableLayout
                android:id="@+id/plan_a_trip_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tableRow1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp" 
                android:stretchColumns="0,1,2,3">


            </TableLayout>

            <Button
                android:id="@+id/plan_a_trip_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/plan_a_trip_table"
                android:layout_centerHorizontal="true"
                android:text="Plan Trip" 
                android:layout_marginTop="20dp"/>

        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

输出

The output

预期输出

Expected output

我应该怎么做才能将复选框,图像视图和文本框置于同一行,使它们看起来不像上面的输出那么杂乱?

1 个答案:

答案 0 :(得分:1)

在您的代码中,您还需要将TableRowCENTER对齐,

 TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        rowParams.gravity = Gravity.CENTER;

并为所有子视图设置layoutParams

即。简单地说,尝试使用以下代码更改planTripBody函数,它将起作用。

 public void planTripBody(String imagename, String nameType,
            String placeId) {

        tableRowCount = tableRowCount + 1;


        // ----------------plan_a_trip_table table
        // body------------------------------------------

        TableRow plan_trip_tr_data = new TableRow(this);

        TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        rowParams.gravity = Gravity.CENTER;
        plan_trip_tr_data.setLayoutParams(rowParams);

        plan_trip_tr_data.setId(10);
        //plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
        plan_trip_tr_data.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TableRow.LayoutParams innerParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  

        // select checkbox
        final CheckBox select_checkbox = new CheckBox(this);
        select_checkbox.setId(20);
        select_checkbox.setLayoutParams(innerParams);
        select_checkbox.setTextColor(Color.BLACK);
        select_checkbox.setGravity(Gravity.CENTER);
        select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        plan_trip_tr_data.addView(select_checkbox);

        // image
        final ImageView place_imageview = new ImageView(this);
        place_imageview.setId(20);
        place_imageview.setLayoutParams(innerParams);

        // test_name.setText(parser.getValue(e, KEY_SETNAME));
        //place_imageview.setPadding(5, 5, 5, 5);
        place_imageview.setImageResource(R.drawable.planatrip);
        plan_trip_tr_data.addView(place_imageview);// add the column to
                                                        // the table row
                                                        // here

        // Type
        final TextView name_type = new TextView(this);
        name_type.setLayoutParams(innerParams);

        name_type.setId(21);// define id that must be unique
        // no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
        // the text for the header
        name_type.setText(Html.fromHtml(nameType));
        name_type.setTextColor(Color.BLACK); // set the color
        name_type.setPadding(5, 5, 5, 5); // set the padding (if
                                                // required)
        name_type.setGravity(Gravity.LEFT);
        name_type.setTextSize(18);
        plan_trip_tr_data.addView(name_type); // add the column
                                                            // to
                                                            // the table row
                                                            // here


        // ID
        final TextView place_id = new TextView(this);
        place_id.setLayoutParams(innerParams);

        place_id.setId(21);// define id that must be unique
        // total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
        // the text for the header
        place_id.setText(placeId);
        place_id.setTextColor(Color.BLACK); // set the color
        place_id.setPadding(5, 5, 5, 5); // set the padding (if
                                                // required)
        place_id.setGravity(Gravity.CENTER);
        place_id.setTextSize(10);
        plan_trip_tr_data.addView(place_id); // add the column
                                                            // to the
        // table row here


        plan_a_trip_table.addView(plan_trip_tr_data,
                new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        // ----------------------On click name_type

        name_type.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (!select_checkbox.isChecked()) {
                    select_checkbox.setChecked(true);
                } else {
                    select_checkbox.setChecked(false);
                }

            }
        });
        // ----------------------On click customer_name



    }

修改

您需要将android:gravity="center_vertical"属性设置为 TableRow ,以使其子项垂直居中。
并使用TableRow高度为android:layout_height="match_parent"
并将Padding设置为表格行android:paddingTop="10dp",不要将保证金设置为Textview

所以将您的布局更改为以下代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:paddingTop="10dp"
                android:gravity="center_vertical"
                android:layout_centerHorizontal="true" >

                <TextView
                    android:id="@+id/city_name_textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="City name"
                    android:textSize="20sp" />

                <ImageView
                    android:id="@+id/update_indicator_imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@android:drawable/presence_busy" 
                    android:layout_marginLeft="10dp"/>
            </TableRow>

            <TableLayout
                android:id="@+id/plan_a_trip_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tableRow1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp" 
                android:stretchColumns="0,1,2,3">


            </TableLayout>

            <Button
                android:id="@+id/plan_a_trip_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/plan_a_trip_table"
                android:layout_centerHorizontal="true"
                android:text="Plan Trip" 
                android:layout_marginTop="20dp"/>

        </RelativeLayout>
    </ScrollView>

</RelativeLayout>