Группа переключателей в макете таблицы с переключателями, выровненными по столбцам

Я разрабатываю приложение обратной связи, которое должно иметь макет, аналогичный этому экрану. [1]: https://i.stack.imgur.com/xn3kh.jpg

Я разработал xml для макета.

<LinearLayout 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:background="#000"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.zankrutparmar.feedback.Frag_one">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/frag_one_title"
    android:textColor="#fff"
    android:textStyle="bold|italic"
    android:textSize="35dp"
    android:textAlignment="center"/>

<LinearLayout
    android:layout_width="819dp"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_vertical_margin"
        android:text="Poor"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="35dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:text="Average"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="35dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:text="Good"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="35dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:text="Very Good"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="35dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:text="Excellent"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="35dp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="6">

    <TextView
        android:layout_width="58dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_weight="1"
        android:text="Theme &amp; Decor"
        android:textAlignment="center"
        android:textColor="#fff"
        android:textSize="25dp"
        android:textStyle="bold|italic" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_weight="5"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/poor1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/custom_btn_radio_poor"
            android:button="@null" />

        <RadioButton
            android:id="@+id/average1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/custom_btn_radio_average"
            android:button="@null" />

        <RadioButton
            android:id="@+id/good1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/custom_btn_radio_good"
            android:button="@null" />

        <RadioButton
            android:id="@+id/very1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/custom_btn_radio_very_good"
            android:button="@null" />

        <RadioButton
            android:id="@+id/excellent1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/custom_btn_radio_excellent"
            android:button="@null" />
    </RadioGroup>
</LinearLayout>

Now whenever I want to apply margin to these radio buttons the center one remains in center and pushes all other buttons far.

Я также пробовал ответы на этот вопрос [1]: радиогруппы с соответствующими метками, выровненными в android

Но в этом макете проблема заключается в том, что выбираются все кнопки в одной строке. Это означает, что Radiogroup не может быть размещена в этом макете.

И я должен получить вопросы из базы данных и поместить их в свое приложение. Так что в будущем мне придется динамически добавлять строки. Любая помощь будет очень признательна. Заранее спасибо.


person Zankrut Parmar    schedule 23.01.2018    source источник
comment
Попробуйте уменьшить layout_width и layout_height RadioButton.   -  person R G    schedule 23.01.2018
comment
Я использую пользовательский рисунок в качестве фона переключателя, поэтому ширина и высота являются обязательными. И я попробовал ваш ответ, но это не сработало.   -  person Zankrut Parmar    schedule 23.01.2018


Ответы (1)


Хорошо, я разработал новый макет и исправил предыдущие проблемы. Я загружаю этот код.

<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:background="#8bae3a"
tools:context="com.zankrutparmar.feedback.Frag_two">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView5"
            android:layout_column="1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView

            android:id="@+id/textView9"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </TableRow>

    <TableRow
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RadioGroup
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:orientation="horizontal">

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:padding="8dp"
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <RadioButton

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="5dp"
                />

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="7dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />
        </RadioGroup>
    </TableRow>

    <TableRow

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <RadioGroup
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:orientation="horizontal">

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:padding="8dp"
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <RadioButton

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="5dp"
                />

            <RadioButton
                style="@style/MyRadioButtonStyle"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:layout_marginLeft="7dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />
        </RadioGroup>
    </TableRow>

</TableLayout>

person Zankrut Parmar    schedule 23.01.2018
comment
Я бы использовал android:gravity="center", и это решило бы мою проблему - person Fernando Torres; 17.10.2019