2016-02-19 50 views
0

堆放在彼此的頂部我必須把我的單位轉換器應用到片段,並有工作。所有東西都編譯好了,所以我可以在虛擬機中運行它。唯一的問題是,所有的XML視覺效果都堆疊在一起。我將提供代碼和虛擬機中的外觀圖片。請幫我解決我的問題,以免它們堆疊在一起。片段Android Studio中在虛擬機

content_unit_converter.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.plamen.unitconverter.UnitConverterActivity" 
tools:showIn="@layout/activity_unit_converter"> 

    <fragment 
    android:id="@+id/unitconverterfragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:name="com.plamen.unitconverter.UnitConverterFragment"/> 
<fragment 
    android:id="@+id/unitcoverterdisplay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:name="com.plamen.unitconverter.UnitConverterDisplay"/> 

</RelativeLayout> 

fragment_unit_converter.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.plamen.unitconverter.UnitConverterActivity" 
tools:showIn="@layout/activity_unit_converter"> 

<RadioGroup 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/TemperatureEditText" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/radioGroup"> 


    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="From Celsius to Farenheit" 
     android:id="@+id/toFarenheitRadioButton" 
     android:checked="true" /> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="From Farenheit to Celsius" 
     android:id="@+id/toCelsiusRadioButton" 
     android:checked="false" /> 
</RadioGroup> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Convert" 
    android:id="@+id/convertButton" 
    android:layout_below="@+id/radioGroup" 
    android:layout_centerHorizontal="true"/> 

<Spinner 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/spinner1" 
    android:spinnerMode="dropdown" 
    android:layout_below="@+id/LengthEditText" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="false" 
    android:layout_alignRight="@+id/LengthEditText" 
    android:layout_alignEnd="@+id/LengthEditText" 
    android:entries ="@array/length" 

    /> 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="CONVERT" 
    android:id="@+id/convertLength" 
    android:layout_below="@+id/spinner1" 
    android:layout_centerHorizontal="true"/> 

<Spinner 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/spinner2" 
    android:layout_below="@+id/AreaEditText" 
    android:layout_centerHorizontal="true" 
    android:entries ="@array/area" 
    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Convert" 
    android:id="@+id/convertArea" 
    android:layout_below="@+id/spinner2" 
    android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

fragment_unit_converter_display.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.plamen.unitconverter.UnitConverterActivity" 
tools:showIn="@layout/activity_unit_converter"> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:ems="10" 
    android:id="@+id/TemperatureEditText" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:hint="Enter Temperature" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:ems="10" 
    android:id="@+id/LengthEditText" 
    android:hint="Enter Length" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:ems="10" 
    android:id="@+id/AreaEditText" 
    android:layout_below="@+id/convertLength" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:hint="Enter Area" /> 

</RelativeLayout> 

這是個什麼樣子,當我在虛擬機上運行它像

enter image description here

這是什麼樣子後,我試過的android:layout_below 它還將被複制,我不知道爲什麼。

enter image description here

+0

那麼,在'content_unit_converter.xml'定義你的兩個片段是在一個'RelativeLayout',因此他們簡單地放置在彼此的頂部 - 就像它會與任何看法。您可能只想顯示一個,因此在需要時使用片段事務來隱藏或顯示相關片段。或者,可能更好,動態實例化第二個片段,並使用事務添加/替換它。或者,如果兩者應該同時可見,只需添加規則將一個位置放在另一個之下(儘管在這種情況下'LinearLayout'可能更合適)。 –

+0

您希望佈局看起來如何?你能提供一個樣機圖紙嗎? –

回答

2

一般情況下,只要您使用相對佈局必須指定事情。在線性佈局中,添加新物體會將其放置在前一個元素的下方/旁邊(取決於您的方向),但在相對佈局中,這不會發生。

所以,你有這樣的:

<fragment 
android:id="@+id/unitconverterfragment" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:name="com.plamen.unitconverter.UnitConverterFragment" 

/> 
<fragment 
android:id="@+id/unitcoverterdisplay" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:name="com.plamen.unitconverter.UnitConverterDisplay" 
/> 

在這裏你只顯示2個片段,但沒有說哪一個先行在那裏。

你可以加入這一行,或類似:

android:layout_below="@id/unitconverterfragment" 

所以,現在你也會有這樣的:

<fragment 
android:id="@+id/unitconverterfragment" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:name="com.plamen.unitconverter.UnitConverterFragment" 

/> 
<fragment 
android:id="@+id/unitcoverterdisplay" 
android:layout_below="@id/unitconverterfragment" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:name="com.plamen.unitconverter.UnitConverterDisplay" 
/> 

您可以在下面放置它,右,左,上頂等等......

+0

我用你的建議添加了一張新照片。 – Plam

+0

是啊...將相同的東西應用於編輯文本和每個片段內的所有元素。你必須告訴佈局你想要單獨的東西 – TooManyEduardos