jeudi 23 février 2017

Using layout_weight for vertical orientated inner LinearLayouts

I would like to have maximum 5 CheckBoxes taking equal amount of available space among each other. The amount of CheckBoxes depends on how many answers a question have. So I'm generating the CheckBoxes dynamically but the maximum is 5.

enter image description here

My main LinearLayout has a horizontal orientation and the 2 inner LinearLayout have a vertical orientation. I tried using android:weightSum=5 for the both of the inner Layouts and android:layout_weight="1" for each CheckBox. But it doesn't work.

 <LinearLayout
    android:id="@+id/layout_multiplechoice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/layout_question"
    android:layout_margin="5dp"
    android:layout_toEndOf="@+id/iv_image"
    android:layout_toRightOf="@+id/iv_image"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/layout_multiplechoice_solutions"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        android:visibility="invisible"
        android:weightSum="5">

        <CheckBox
            android:id="@+id/cb_solution1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"/>

        <CheckBox
            android:id="@+id/cb_solution2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"/>

        <CheckBox
            android:id="@+id/cb_solution3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"/>

        <CheckBox
            android:id="@+id/cb_solution4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"/>

        <CheckBox
            android:id="@+id/cb_solution5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:enabled="false"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_multiplechoice_answers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="5">

        <CheckBox
            android:id="@+id/cb_answer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="@android:color/holo_green_light"/>

        <CheckBox
            android:id="@+id/cb_answer2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="@android:color/holo_green_light"/>

        <CheckBox
            android:id="@+id/cb_answer3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="@android:color/holo_green_light"/>

        <CheckBox
            android:id="@+id/cb_answer4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="@android:color/holo_green_light"/>

        <CheckBox
            android:id="@+id/cb_answer5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="@android:color/holo_green_light"/>
    </LinearLayout>
</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire