【问题标题】:How to move RadioGroup below specified EditText Android Dev如何将 RadioGroup 移动到指定的 EditText Android Dev下方
【发布时间】:2017-11-22 18:19:22
【问题描述】:

我正在学习 Android 应用开发。我的问题是radioButtons 在我的editText1 和editText2 上。我梦见了这个订单:

  1. editText 与第一个数字
  2. 用第二个数字编辑文本
  3. 单选按钮 +
  4. 单选按钮 -
  5. 单选按钮 *
  6. 单选按钮/
  7. 文本查看结果
  8. 按钮

第 (i+1) 个元素在第 i 个元素之下。

我想知道我应该怎么做,因为当我将 android:layout_below="@+id/editText2" 添加到 radioGroup 时,它没有任何作用。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.addme.calculator.MainActivity">

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

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="first number"
            android:layout_alignParentTop="true"
            android:inputType="number" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText1"
            android:hint="second number"
            android:inputType="number" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioGroup">
            android:layout_below="@+id/editText2"
            <RadioButton
                android:id="@+id/radioPlus"
                android:layout_below="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:text="+" />

            <RadioButton
                android:layout_below="@+id/radioPlus"
                android:id="@+id/radioMinus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-" />

            <RadioButton
                android:id="@+id/radioMult"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_below="@+id/radioMinus"
                android:text="*" />

            <RadioButton
                android:id="@+id/radioDiv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="/"
                android:layout_below="@+id/radioMult"
                android:layout_above="@+id/button" />

        </RadioGroup>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:onClick="onButtonClick"
            android:text="Button"

            />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioGroup"
            android:text="Result" />



    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 使用android:layout_below

标签: android xml calculator android-relativelayout


【解决方案1】:

在您的布局 xml 中,此属性 android:layout_below="@+id/editText2" 位于无线电组标记之外。检查一下

【讨论】:

  • 谢谢,这是一个愚蠢的错误。现在一切正常。
【解决方案2】:

您的 xml 在单选组中的 xml 中有错误:

android:layout_below="@+id/editText2"

改成下面这样:

    <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioGroup"
android:layout_below="@+id/editText2">

【讨论】:

    【解决方案3】:

    android:layout_below="@+id/editText2" 在无线电组标签之外。使用以下代码更改您的代码

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:hint="first number"
            android:inputType="number" />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText1"
            android:hint="second number"
            android:inputType="number" />
    
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText2">
    
            <RadioButton
                android:id="@+id/radioPlus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText2"
                android:layout_weight="1"
                android:checked="true"
                android:text="+" />
    
            <RadioButton
                android:id="@+id/radioMinus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/radioPlus"
                android:layout_weight="1"
                android:text="-" />
    
            <RadioButton
                android:id="@+id/radioMult"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/radioMinus"
                android:layout_weight="1"
                android:text="*" />
    
            <RadioButton
                android:id="@+id/radioDiv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/button"
                android:layout_below="@+id/radioMult"
                android:layout_weight="1"
                android:text="/" />
    
        </RadioGroup>
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:onClick="onButtonClick"
            android:text="Button"
    
            />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioGroup"
            android:text="Result" />
    
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      试试这个,radioButtons 在 RadioGroup 中自动对齐

      <RadioGroup
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/radioGroup"
              android:layout_below="@+id/editText2">
              <RadioButton
                  android:id="@+id/radioPlus"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:checked="true"
                  android:text="+" />
      
              <RadioButton
                  android:id="@+id/radioMinus"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="-" />
      
              <RadioButton
                  android:id="@+id/radioMult"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="*" />
      
              <RadioButton
                  android:id="@+id/radioDiv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="/" />
      
          </RadioGroup>
      

      【讨论】:

        【解决方案5】:

        你为什么还要使用RelativeLayout?您可以使用 LinearLayout(Vertical) 作为所有视图的容器。这是最简单的方法。

        【讨论】:

          猜你喜欢
          • 2017-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多