2017-06-20 103 views
-1

我正在學習Android App Dev。我的問題是radioButtons在我的editText1和editText2上。我夢見順序如下:如何將RadioGroup移動到指定位置EditText Android Dev

  1. EDITTEXT與第一數
  2. EDITTEXT與第二數
  3. 單選按鈕+
  4. 單選按鈕 -
  5. 單選按鈕*
  6. 單選按鈕/
  7. textView結果
  8. BUTTON

其中第(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> 
+0

使用'android:layout_below' –

回答

1

在您的佈局xml此屬性android:layout_below="@+id/editText2"是無線電組標記之外。檢查它

+0

謝謝,這是一個愚蠢的錯誤。現在一切正常。 – user7396096

+0

Nvr心靈......發生:-) – Moulesh

0

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

試試這個,在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> 
+0

它拋出錯誤我的朋友 –

1

單選按鈕自動對齊有在行在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"> 
0

爲什麼你甚至使用RelativeLayout?您可以使用LinearLayout(垂直)作爲所有視圖的容器。這是最簡單的方法。

相關問題