Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android studio中正确对齐单选按钮?_Android_Android Studio_Radio Button_Radio Group - Fatal编程技术网

如何在android studio中正确对齐单选按钮?

如何在android studio中正确对齐单选按钮?,android,android-studio,radio-button,radio-group,Android,Android Studio,Radio Button,Radio Group,我有三个广播组,每个广播组内有两个按钮。正如您可以从这张图片中看到的:[1],在android studio中,所有按钮都排列得很好。然而,当我在虚拟机中运行应用程序时,我的结果是:[2] 你知道为什么会这样吗?提前感谢您的帮助 xml代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x

我有三个广播组,每个广播组内有两个按钮。正如您可以从这张图片中看到的:[1],在android studio中,所有按钮都排列得很好。然而,当我在虚拟机中运行应用程序时,我的结果是:[2]

你知道为什么会这样吗?提前感谢您的帮助

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="onerepmax.reversepyramid.RPworkout">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/workoutButton"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:width="175dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/warmupButton"
        android:id="@+id/button3"
        android:width="175dp"
        android:layout_alignBottom="@+id/button"
        android:layout_toRightOf="@+id/button"
        android:layout_toEndOf="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/liftText"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="xxx lbs"
        android:id="@+id/weightText1"
        android:layout_marginRight="36dp"
        android:layout_marginEnd="36dp"
        android:layout_alignTop="@+id/repText1"
        android:layout_toLeftOf="@+id/repText1"
        android:layout_toStartOf="@+id/repText1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="XX reps"
        android:id="@+id/repText1"
        android:layout_marginTop="66dp"
        android:layout_below="@+id/liftText"
        android:layout_alignLeft="@+id/liftText"
        android:layout_alignStart="@+id/liftText"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="xxx lbs"
        android:id="@+id/weightText2"
        android:layout_alignTop="@+id/weightText1"
        android:layout_marginTop="70dp"
        android:layout_alignLeft="@+id/weightText3"
        android:layout_alignStart="@+id/weightText3" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="XX reps"
        android:id="@+id/repText2"
        android:layout_alignTop="@+id/repText1"
        android:layout_alignLeft="@+id/repText1"
        android:layout_alignStart="@+id/repText1"
        android:layout_marginTop="70dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="xxx lbs"
        android:id="@+id/weightText3"
        android:layout_alignTop="@+id/weightText2"
        android:layout_marginTop="70dp"
        android:layout_alignLeft="@+id/weightText1"
        android:layout_alignStart="@+id/weightText1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="XX reps"
        android:id="@+id/repText3"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/repText1"
        android:layout_alignStart="@+id/repText1"
        android:layout_alignTop="@+id/repText2"
        android:layout_marginTop="70dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nextButton"
        android:id="@+id/nextButton"
        android:layout_marginBottom="39dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignRight="@+id/button3"
        android:layout_alignEnd="@+id/button3"
        android:layout_above="@+id/weightText3"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/nextButton"
        android:layout_toEndOf="@+id/nextButton"
        android:layout_below="@+id/liftText"
        android:id="@+id/radioGroup">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton2"
            android:checked="false"
            android:buttonTint="#2ebb39"
            android:layout_marginTop="59dp"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton"
            android:checked="false"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="29dp"
            android:layout_marginTop="59dp"
            android:buttonTint="#d32323"/>

    </RadioGroup>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignRight="@+id/radioGroup"
        android:layout_alignEnd="@+id/radioGroup"
        android:layout_below="@+id/weightText1"
        android:layout_alignBottom="@+id/weightText3"
        android:orientation="horizontal"
        android:layout_toEndOf="@+id/button"
        android:layout_toRightOf="@+id/button">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton3"
            android:checked="false"
            android:buttonTint="#2ebb39"
            android:layout_marginTop="45dp"
            android:layout_marginLeft="45dp"
            android:layout_marginStart="45dp"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton4"
            android:checked="false"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="29dp"
            android:layout_marginTop="45dp"
            android:buttonTint="#d32323"/>
    </RadioGroup>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignRight="@+id/radioGroup"
        android:layout_alignEnd="@+id/radioGroup"
        android:layout_below="@+id/repText2"
        android:layout_above="@+id/nextButton">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton5"
            android:checked="false"
            android:buttonTint="#2ebb39"
            android:layout_marginTop="45dp"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="29dp"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton6"
            android:checked="false"
            android:layout_marginRight="37dp"
            android:layout_marginEnd="37dp"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="29dp"
            android:layout_marginTop="45dp"
            android:buttonTint="#d32323"/>
    </RadioGroup>

</RelativeLayout>

这将解决对齐问题,也无需为文本视图添加标签,而是执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="onerepmax.reversepyramid.RPworkout">

    <!-- Even spaced buttons above -->
    <LinearLayout
        android:id="@+id/btn_wrapper"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Workout"
            android:id="@+id/button"
            android:layout_weight="1"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Warmup"
            android:id="@+id/button3"
            android:layout_weight="1"/>

    </LinearLayout>

    <!-- Centered Text -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/liftText"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/btn_wrapper"
        android:layout_centerHorizontal="true" />

    <!-- Centered Radio Group with Label 1 -->
    <RelativeLayout
        android:id="@+id/set_1"
        android:layout_below="@+id/liftText"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeight">

        <TextView
            android:id="@+id/txt_lbs_1"
            android:text="xxx lbs xx reps"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:layout_toRightOf="@+id/txt_lbs_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerVertical="true"
            android:id="@+id/radioGroup">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:layout_marginLeft="16dp"
                android:checked="false"
                android:buttonTint="#2ebb39"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton"
                android:checked="false"
                android:layout_marginLeft="16dp"
                android:buttonTint="#d32323"/>

        </RadioGroup>
    </RelativeLayout>

    <!-- Centered Radio Group with Label 2 -->
    <RelativeLayout
        android:id="@+id/set_2"
        android:layout_below="@+id/set_1"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeight">

        <TextView
            android:id="@+id/txt_lbs_2"
            android:text="xxx lbs xx reps"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:layout_toRightOf="@+id/txt_lbs_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="horizontal"
            android:id="@+id/radioGroup2">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButtonPassTwo"
                android:layout_marginLeft="16dp"
                android:checked="false"
                android:buttonTint="#2ebb39"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButtonFailTwo"
                android:checked="false"
                android:layout_marginLeft="16dp"
                android:buttonTint="#d32323"/>

        </RadioGroup>
    </RelativeLayout>

    <!-- Centered Radio Group with Label 3 -->
    <RelativeLayout
        android:id="@+id/set_3"
        android:layout_below="@+id/set_2"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeight">

        <TextView
            android:id="@+id/txt_lbs_3"
            android:text="xxx lbs xx reps"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:layout_toRightOf="@+id/txt_lbs_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="horizontal"
            android:id="@+id/radioGroup3">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButtonPassThree"
                android:layout_marginLeft="16dp"
                android:checked="false"
                android:buttonTint="#2ebb39"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButtonFailThree"
                android:checked="false"
                android:layout_marginLeft="16dp"
                android:buttonTint="#d32323"/>

        </RadioGroup>
    </RelativeLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Workout"
        android:id="@+id/nextButton"
        android:layout_marginBottom="39dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
这将产生:

祝你好运,希望这能为你指明一个更好的方向


快乐编码

共享此版面的完整xml,显示没有实际对齐元素的单选组没有多大帮助确定,编辑为包含完整xml文件。对于初学者,如果没有正确使用单选组,可以使用RadioButton提供的文本元素大大改进版面。我无法想象这会如何工作。在使用提供的文本元素时,如何获得我现在拥有的UI?此外,我不认为没有文字就意味着我没有正确地使用它们。问题是,就应用程序而言,这是没有意义的。本例中的单选按钮为通过/失败按钮。将重量放在“通过”按钮旁边,将重复次数放在“失败”按钮旁边是没有意义的。啊@crseat我明白了,我错过了。目的将在few@crseat请参阅上面的更新答案,很抱歉出现混淆。是的,此问题已解决。由于程序的工作方式,我确实需要两个文本视图,但即使在我将其他文本视图添加到代码中后,其工作效果仍然比我的要好得多。我想这与我对广播集团的塑造有多差有关。谢谢
 TextView mTextViewLbs1 = (TextView) findViewById(R.id.txt_lbs_1);
 mTextViewLbs1.setText(String.format("%1$d lbs %2$d reps", 200, 3));