2012-03-05 9 views
6

Mam układ, który jestem nadmuchiwania, aby dynamicznie dodać TableRows do TableLayout. Używam layout_weight do rozciągania kolumn do pożądanej szerokości.Center View (CheckBox), który używa layout_weight w TableRow kolumna

Tabela umieszczana jest wewnątrz ScrollView, dzięki czemu mogę przewijać między wygenerowanymi wierszami. Mam również nagłówek tabeli, który umieszczam na wierzchu w postaci LinearLayout. Zrobiłem to, ponieważ nie chcę przewijać nagłówka. Właśnie dlatego nie używam layout_span zamiast layout_weight.

Problem polega na tym, że jeden z widoków w TableRow to CheckBox i chcę go wyśrodkować, ale ponieważ atrybut layout_width to "0", nie mogę użyć layout_gravity = "center", aby go wyśrodkować.

Tutaj jest plik xml:

<?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> 

Jak to wygląda: CheckBox not centered

ten sposób chcę wyglądać: enter image description here

* Kolory są za reprezentacyjne tylko cel. Można je zignorować.

Byłbym bardzo wdzięczny, gdyby ktoś mógł pomóc. Dziękuję

Odpowiedz

21

spróbuj tego:

<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> 
+1

Dziękuję. Ustawiłeś mnie na właściwej drodze. Skończyło się na użyciu RelativeLayout z layout_width = "0dp", ponieważ konieczne było użycie szerokości 0 i RelativeLayout jest bardziej lekki niż LinearLayout. Wydaje się, że nie ma zauważalnego wpływu na wydajność. Jeszcze raz dziękuję – Bandreid

+3

To wydaje się działać, ale dlaczego zamykanie pola wyboru w innym layoucie jest jedynym działającym rozwiązaniem? Wygląda bardziej na włamanie niż na właściwy sposób. –

Powiązane problemy