【问题标题】:How do you properly align radio buttons in android studio?你如何正确对齐android studio中的单选按钮?
【发布时间】:2016-04-08 17:44:54
【问题描述】:

我有三个收音机组,每个组里面都有两个按钮。从这张图片可以看出:http://imgur.com/JQJ0a7A [1] ,按钮在android studio中都排得很整齐。但是,当我在 VM 中运行应用程序时,我最终会得到:http://imgur.com/nKdth41 [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,显示没有它们实际对齐的元素的单选组并不是很有帮助
  • 好的,已编辑以包含完整的 xml 文件。
  • 对于没有正确使用单选组的初学者,您可以使用 RadioButton 提供的文本元素大大改进您的布局
  • 我无法想象它会如何工作。在使用提供的文本元素时,您将如何获得我现在拥有的 UI?此外,我不认为没有文本意味着我使用错误。

标签: android android-studio radio-button radio-group


【解决方案1】:

这应该可以解决您的对齐问题,也不需要标记文本视图,而是这样做:

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

当您想使用 txt_lbs_1 为 TextView 设置类似“200 lbs 10 reps”的内容时

做:

 TextView mTextViewLbs1 = (TextView) findViewById(R.id.txt_lbs_1);
 mTextViewLbs1.setText(String.format("%1$d lbs %2$d reps", 200, 3)); 

这会产生:

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

编码愉快!

【讨论】:

  • 问题在于,就应用程序而言,这没有意义。在这种情况下,单选按钮是通过/失败按钮。将重量放在通过按钮旁边,将代表放在失败按钮旁边是没有意义的。
  • 啊@crseat 我看到我想念那里的解释,目的将在几个之后提供更新
  • @crseat 请参阅上面的更新答案,很抱歉造成混淆
  • 是的,这修复了它。由于程序的工作方式,我实际上确实需要两个文本视图,但即使在我将其他文本视图添加回您的代码中之后,它的效果仍然比我的要好得多。我认为这与我对广播团体的塑造有多差有关。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 2022-12-04
  • 2011-05-12
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
相关资源
最近更新 更多