Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在tablerow中将imageview垂直居中对齐_Java_Android_Row_Android Imageview_Android Tablelayout - Fatal编程技术网

Java 在tablerow中将imageview垂直居中对齐

Java 在tablerow中将imageview垂直居中对齐,java,android,row,android-imageview,android-tablelayout,Java,Android,Row,Android Imageview,Android Tablelayout,我已经创建了一个动态表。该表应该有一行,在左边会有一个图像视图,中间有一个文本视图,右边是一个复选框。所有的控件都显示出来了。textview和复选框也垂直居中,但inageview无法居中,因此该行看起来不好我想将imageview与textview和复选框排成一行。 上图显示了我现在得到的输出。我希望蓝色箭头与textview和复选框对齐 活动的源代码: public class CalendarActivity extends Activity { TableLayout fi

我已经创建了一个动态表。该表应该有一行,在左边会有一个图像视图,中间有一个文本视图,右边是一个复选框。所有的控件都显示出来了。textview和复选框也垂直居中,但inageview无法居中,因此该行看起来不好我想将imageview与textview和复选框排成一行。

上图显示了我现在得到的输出。我希望蓝色箭头与textview和复选框对齐

活动的源代码:

public class CalendarActivity extends Activity {

    TableLayout first_calendar_table;
    TableRow first_calendar_tr_data;
    int tableRowCount = 0;
    boolean showingFirst = true;

    String chkChecked = "false";

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

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

        tableHeads();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.calendar, menu);
        return true;
    }

    // method to generate table heads
    public void tableHeads() {

        // ---------------Calendar Table
        // Header-----------------------------------------------
        TableRow supplier_details_tr_head = new TableRow(this);
        supplier_details_tr_head.setId(10);
        supplier_details_tr_head.setBackgroundResource(R.drawable.list_header);
        supplier_details_tr_head.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        // ImageView Setup
        final ImageView imageView = new ImageView(this);
        // setting image resource
        imageView.setImageResource(R.drawable.closed_icon);
        imageView.setId(20);
        //imageView.setGravity(Gravity.CENTER);
        // adding view to layout
        supplier_details_tr_head.addView(imageView);

        TextView select_head = new TextView(this);
        select_head.setId(20);
        select_head.setText(Html.fromHtml("2014-2015"));
        select_head.setTextColor(Color.WHITE);
        select_head.setGravity(Gravity.CENTER);
        select_head.setPadding(5, 5, 5, 5);
        supplier_details_tr_head.addView(select_head);// add the column to
                                                        // the
        // table row here
        select_head.setTextSize(20);

        // select checkbox
        final CheckBox customer_checkbox = new CheckBox(this);
        customer_checkbox.setId(20);
        customer_checkbox.setTextColor(Color.BLACK);
        customer_checkbox.setGravity(Gravity.CENTER);
        customer_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        customer_checkbox.setButtonDrawable(R.drawable.checkbox_selector);
        supplier_details_tr_head.addView(customer_checkbox);

        first_calendar_table.addView(supplier_details_tr_head,
                new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        // ---------------Calendar Table
        // Header-----------------------------------------------

        // ----------------------On click imageView

        imageView.setOnClickListener(new OnClickListener() {

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


                if(showingFirst == true){
                    imageView.setImageResource(R.drawable.opened_icon);
                    showingFirst = false;
                }else{
                    imageView.setImageResource(R.drawable.closed_icon);
                    imageView.setTag(70);
                    showingFirst = true;
                }

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

    // method to show toast message
        public void makeAToast(String str) {
            // yet to implement
            Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="3dp"
    tools:context=".CalendarActivity" >

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

        <RelativeLayout
            android:id="@+id/CalendarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#FFFFFF" >

            <TableLayout
                android:id="@+id/first_calendar_table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/second_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/first_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/third_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/second_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/fourth_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/third_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>
布局文件:

public class CalendarActivity extends Activity {

    TableLayout first_calendar_table;
    TableRow first_calendar_tr_data;
    int tableRowCount = 0;
    boolean showingFirst = true;

    String chkChecked = "false";

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

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

        tableHeads();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.calendar, menu);
        return true;
    }

    // method to generate table heads
    public void tableHeads() {

        // ---------------Calendar Table
        // Header-----------------------------------------------
        TableRow supplier_details_tr_head = new TableRow(this);
        supplier_details_tr_head.setId(10);
        supplier_details_tr_head.setBackgroundResource(R.drawable.list_header);
        supplier_details_tr_head.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        // ImageView Setup
        final ImageView imageView = new ImageView(this);
        // setting image resource
        imageView.setImageResource(R.drawable.closed_icon);
        imageView.setId(20);
        //imageView.setGravity(Gravity.CENTER);
        // adding view to layout
        supplier_details_tr_head.addView(imageView);

        TextView select_head = new TextView(this);
        select_head.setId(20);
        select_head.setText(Html.fromHtml("2014-2015"));
        select_head.setTextColor(Color.WHITE);
        select_head.setGravity(Gravity.CENTER);
        select_head.setPadding(5, 5, 5, 5);
        supplier_details_tr_head.addView(select_head);// add the column to
                                                        // the
        // table row here
        select_head.setTextSize(20);

        // select checkbox
        final CheckBox customer_checkbox = new CheckBox(this);
        customer_checkbox.setId(20);
        customer_checkbox.setTextColor(Color.BLACK);
        customer_checkbox.setGravity(Gravity.CENTER);
        customer_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        customer_checkbox.setButtonDrawable(R.drawable.checkbox_selector);
        supplier_details_tr_head.addView(customer_checkbox);

        first_calendar_table.addView(supplier_details_tr_head,
                new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        // ---------------Calendar Table
        // Header-----------------------------------------------

        // ----------------------On click imageView

        imageView.setOnClickListener(new OnClickListener() {

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


                if(showingFirst == true){
                    imageView.setImageResource(R.drawable.opened_icon);
                    showingFirst = false;
                }else{
                    imageView.setImageResource(R.drawable.closed_icon);
                    imageView.setTag(70);
                    showingFirst = true;
                }

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

    // method to show toast message
        public void makeAToast(String str) {
            // yet to implement
            Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="3dp"
    tools:context=".CalendarActivity" >

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

        <RelativeLayout
            android:id="@+id/CalendarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#FFFFFF" >

            <TableLayout
                android:id="@+id/first_calendar_table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/second_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/first_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/third_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/second_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>

            <TableLayout
                android:id="@+id/fourth_calendar_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/third_calendar_table"
                android:layout_centerHorizontal="true" >
            </TableLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>


我应该如何实现我的目标?

在布局中使用
居中垂直
…在ImageView的父布局中在TableLayout的布局中使用居中垂直..对输出没有影响