View center (CheckBox) that uses layout_weight in the TableRow column

I have a layout that I am inflating to dynamically add TableRows to TableLayout . I use layout_weight to stretch the columns to the desired width.

The table is placed inside the ScrollView , so I can scroll between the generated rows. I also have a table header, which I put in LinearLayout on top. I did this because I do not want to scroll the title. This is why I do not use layout_span instead of layout_weight.

The problem is that one of the views in TableRow is a CheckBox , and I want it to be centered, but since the layout_width attribute is '0' I cannot use layout_gravity = "center" to center it.

Here is the xml file:

<?xml version="1.0" encoding="utf-8"?> <TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/order_form_line_TableRow" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/order_table_row_product_description_TextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.22" android:maxLines="2" android:textSize="18sp" /> <CheckBox android:id="@+id/order_table_row_present_CheckBox" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0.08" /> <EditText android:id="@+id/order_table_row_facings_EditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.10" android:inputType="number" /> <EditText android:id="@+id/order_table_row_free_cu_given_EditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.10" android:inputType="number" /> <EditText android:id="@+id/order_table_row_order_quantity_EditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.10" android:inputType="number" /> <EditText android:id="@+id/order_table_row_free_order_EditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.10" android:inputType="number" /> <Spinner android:id="@+id/order_table_row_wholesaler_Spinner" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.15" /> <EditText android:id="@+id/order_table_row_delivery_date_EditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.15" android:ellipsize="end" android:inputType="date" android:singleLine="true" /> </TableRow> 

Here's what it looks like: Checkbox not centered

Here is how I want it to look: enter image description here

* Colors are for representation purposes only. They can be ignored.

I would really appreciate it if someone could help. Thanks you

+6
source share
1 answer

try the following:

  <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:gravity="center" android:layout_weight="0.08" android:layout_height="fill_parent" > <CheckBox android:id="@+id/order_table_row_present_CheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
+21
source

Source: https://habr.com/ru/post/909984/


All Articles