Radio group in table layout with radio buttons aligned to columns

我的梦境 提交于 2019-12-22 18:33:09

问题


I am developing a feedback application which should has similar layout to this screen. [1]: https://i.stack.imgur.com/xn3kh.jpg

I have designed the xml for layout.

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

I also tried the answers on this question [1]: TableLayout of radiogroup(s) with respective label(s) aligned in android

But in that layout the problem is all buttons in single row are getting selected. It means Radiogroup can't be placed in that layout.

And I have to fetch questions from a database and place them in my application. So in Future I will have to add rows dynamically. Any help will be very appreciated. Thank you in advance.


回答1:


Okay I designed a new layout and fixed those previous issues. I am uploading that code.

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



来源:https://stackoverflow.com/questions/48395008/radio-group-in-table-layout-with-radio-buttons-aligned-to-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!