2016-12-18 2 views
-2

Ich versuche eine App zu erstellen, die ein RelativeLayout in einem ScrollView mit Hilfe von Android Studio hat. Ich habe alle Schaltflächen, Textfelder und Drehfelder hinzugefügt, damit die App so aussieht, wie ich es möchte. Nach dem Debuggen der App änderte RelativeLayout jedoch automatisch seine Höhe, wodurch die Schaltflächen, Textfelder und Drehfelder unbrauchbar wurden.Wie kann ich verhindern, dass RelativeLayout seine Höhe automatisch ändert?

Dies ist ein Bild von der App nach Debugging: App looks messed up

Die XML-Datei wird wie folgt:

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/activity_main" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      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="fadi_christoph.pizzadeliveryapp.MainActivity"> 

      <TextView 
       android:id="@+id/tvAnrede" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_above="@+id/ET_Name" 
       android:layout_alignParentStart="true" 
       android:text="Anrede" 
       android:textAlignment="textStart" 
       android:textAllCaps="false" 
       android:textSize="14sp" /> 

      <RadioGroup 
       android:id="@+id/rgAnrede" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_alignStart="@+id/ET_Name" 
       android:orientation="horizontal"> 

       <RadioButton 
        android:id="@+id/rbHerr" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Herr" 
        android:textSize="14sp" /> 

       <RadioButton 
        android:id="@+id/rbFrau" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Frau" 
        android:textSize="14sp" /> 

      </RadioGroup> 

      <TextView 
       android:id="@+id/tvName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/ET_Name" 
       android:layout_alignBottom="@+id/ET_Name" 
       android:layout_alignParentStart="true" 
       android:text="Vor- und Nachname" 
       android:textSize="14sp" /> 

      <EditText 
       android:id="@+id/tfName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_below="@+id/RG_Anrede" 
       android:layout_toEndOf="@+id/TV_Name" 
       android:ems="10" 
       android:hint="Vor- und Nachname" 
       android:inputType="textPersonName" 
       android:textSize="14sp" /> 

      <TextView 
       android:id="@+id/tvPost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/ET_Post" 
       android:layout_alignBottom="@+id/ET_Post" 
       android:layout_alignParentStart="true" 
       android:text="Postleitzahl" 
       android:textSize="14sp" /> 

      <EditText 
       android:id="@+id/tfPost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/ET_Name" 
       android:layout_below="@+id/ET_Name" 
       android:ems="10" 
       android:hint="Postleitzahl" 
       android:inputType="numberDecimal" 
       android:textSize="14sp" /> 

      <TextView 
       android:id="@+id/tvOrt" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/ET_Ort" 
       android:layout_alignBottom="@+id/ET_Ort" 
       android:layout_alignParentStart="true" 
       android:text="Ort" 
       android:textSize="14sp" /> 

      <EditText 
       android:id="@+id/tfOrt" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/ET_Post" 
       android:layout_below="@+id/ET_Post" 
       android:ems="10" 
       android:hint="Ort" 
       android:inputType="textPersonName" 
       android:textSize="14sp" /> 

      <TextView 
       android:id="@+id/tvStrasse" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/ET_Strasse" 
       android:layout_alignBottom="@+id/ET_Strasse" 
       android:layout_alignParentStart="true" 
       android:text="Straße" 
       android:textSize="14sp" /> 

      <EditText 
       android:id="@+id/tfStrasse" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/ET_Ort" 
       android:layout_below="@+id/ET_Ort" 
       android:ems="10" 
       android:hint="Straße" 
       android:inputType="textPersonName" 
       android:textSize="14sp" /> 

      <TextView 
       android:id="@+id/tvHausnummer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/ET_Hausnummer" 
       android:layout_alignBottom="@+id/ET_Hausnummer" 
       android:layout_alignParentStart="true" 
       android:text="Hausnummer" 
       android:textSize="14sp" /> 

      <EditText 
       android:id="@+id/tfHausnummer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/ET_Strasse" 
       android:layout_below="@+id/ET_Strasse" 
       android:ems="10" 
       android:hint="Hausnummer" 
       android:inputType="textPersonName" 
       android:textSize="14sp" /> 

      <TextView 
       android:id="@+id/tvRechnung" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/ET_Hausnummer" 
       android:text="Rechnung" 
       android:textSize="14sp" /> 

      <RadioGroup 
       android:id="@+id/rgRechnung" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignStart="@+id/ET_Hausnummer" 
       android:layout_below="@+id/ET_Hausnummer" 
       android:orientation="horizontal"> 

       <RadioButton 
        android:id="@+id/rbPost" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="per Post" 
        android:textSize="14sp" /> 

       <RadioButton 
        android:id="@+id/rbEmail" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="per Email" 
        android:textSize="14sp" /> 

      </RadioGroup> 

      <TextView 
       android:id="@+id/tvFilter" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/SP_Filter" 
       android:layout_alignParentStart="true" 
       android:text="suche nach" 
       android:textSize="14sp" /> 

      <Spinner 
       android:id="@+id/spinnerFilter" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/RG_Rechnung" 
       android:layout_below="@+id/RG_Rechnung" /> 

      <TextView 
       android:id="@+id/tvPizza" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/SP_Pizza" 
       android:layout_alignParentStart="true" 
       android:text="Auswahl der Pizza" 
       android:textSize="14sp" /> 

      <Spinner 
       android:id="@+id/spinnerPizza" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/SP_Filter" 
       android:layout_below="@+id/SP_Filter" 
       android:spinnerMode="dialog" /> 

      <TextView 
       android:id="@+id/tvZusaetzen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_above="@+id/RG_Bezahlen" 
       android:layout_alignParentStart="true" 
       android:text="Zusätzen" 
       android:textSize="14sp" /> 

      <Spinner 
       android:id="@+id/spinnerZusaetzen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignStart="@+id/SP_Pizza" 
       android:layout_below="@+id/SP_Pizza" 
       android:spinnerMode="dialog" /> 

      <TextView 
       android:id="@+id/tvBezahlen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/SP_Zusaetzen" 
       android:layout_marginTop="11dp" 
       android:text="bezahlen" 
       android:textSize="14sp" /> 

      <RadioGroup 
       android:id="@+id/rgBezahlen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignStart="@+id/SP_Zusaetzen" 
       android:layout_below="@+id/SP_Zusaetzen"> 

       <RadioButton 
        android:id="@+id/rbBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="bar bei Abholung" 
        android:textSize="14sp" /> 

       <RadioButton 
        android:id="@+id/rbKreditkarte" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Kreditkarte" 
        android:textSize="14sp" /> 

       <RadioButton 
        android:id="@+id/rbSofort" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Sofortüberweisung" 
        android:textSize="14sp" /> 

      </RadioGroup> 

      <TextView 
       android:id="@+id/tvPreis" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/RG_Bezahlen" 
       android:layout_marginTop="16dp" 
       android:text="Preis" /> 

      <TextView 
       android:id="@+id/tvEuro" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/TV_Preis" 
       android:layout_alignBottom="@+id/TV_Preis" 
       android:layout_alignStart="@+id/RG_Bezahlen" 
       android:text="0,00 €" 
       android:textSize="24sp" /> 

      <Button 
       android:id="@+id/btnSenden" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/TV_Euro" 
       android:layout_marginTop="18dp" 
       android:text="Senden" /> 

      <Button 
       android:id="@+id/btnZuruecksetzen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/BT_Senden" 
       android:layout_alignBottom="@+id/BT_Senden" 
       android:layout_toEndOf="@+id/BT_Senden" 
       android:text="Zurücksetzen" /> 

     </RelativeLayout> 

    </ScrollView> 

Ein Teilcode meines Programms ist wie folgt:

package fadi_christoph.pizzadeliveryapp; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.RadioButton; 
    import android.widget.Spinner; 
    import android.widget.TextView; 
    import android.view.View; 
    import android.widget.*; 
    import java.lang.String; 


    public class MainActivity extends AppCompatActivity { 

     private TextView tv_Anrede, tv_Name, tv_Post, tv_Ort, tv_Strasse, tv_Hausnummer, tv_Rechnung, tv_Filter, tv_Pizza, tv_Zusaetzen, tv_Bezahlen, tv_Preis, tv_Euro; 
     private RadioGroup rg_Anrede, rg_Rechnung, rg_Bezahlen; 
     private RadioButton rb_Herr, rb_Frau, rb_Post, rb_Email, rb_Bar, rb_Kreditkarte, rb_Sofort; 
     private EditText tf_Name, tf_Post, tf_Ort, tf_Strasse, tf_Hausnummer; 
     private Spinner spinner_Filter, spinner_Pizza, spinner_Zusaetzen; 
     private Button btn_Senden, btn_Zuruecksetzen; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      InitializeApp(); 
     } 

     private void InitializeApp() { 
      tv_Anrede = (TextView) findViewById(R.id.tvAnrede); 
      tv_Name = (TextView) findViewById(R.id.tvName); 
      tv_Post = (TextView) findViewById(R.id.tvPost); 
      tv_Ort = (TextView) findViewById(R.id.tvOrt); 
      tv_Strasse = (TextView) findViewById(R.id.tvStrasse); 
      tv_Hausnummer = (TextView) findViewById(R.id.tvHausnummer); 
      tv_Rechnung = (TextView) findViewById(R.id.tvRechnung); 
      tv_Filter = (TextView) findViewById(R.id.tvFilter); 
      tv_Pizza = (TextView) findViewById(R.id.tvPizza); 
      tv_Zusaetzen = (TextView) findViewById(R.id.tvZusaetzen); 
      tv_Bezahlen = (TextView) findViewById(R.id.tvBezahlen); 
      tv_Preis = (TextView) findViewById(R.id.tvPreis); 
      tv_Euro = (TextView) findViewById(R.id.tvEuro); 
      rg_Anrede = (RadioGroup) findViewById(R.id.rgAnrede); 
      rg_Rechnung = (RadioGroup) findViewById(R.id.rgRechnung); 
      rg_Bezahlen = (RadioGroup) findViewById(R.id.rgBezahlen); 
      rb_Herr = (RadioButton) findViewById(R.id.rbHerr); 
      rb_Frau = (RadioButton) findViewById(R.id.rbFrau); 
      rb_Post = (RadioButton) findViewById(R.id.rbPost); 
      rb_Email = (RadioButton) findViewById(R.id.rbEmail); 
      rb_Bar = (RadioButton) findViewById(R.id.rbBar); 
      rb_Kreditkarte = (RadioButton) findViewById(R.id.rbKreditkarte); 
      rb_Sofort = (RadioButton) findViewById(R.id.rbSofort); 
      tf_Name = (EditText) findViewById(R.id.tfName); 
      tf_Post = (EditText) findViewById(R.id.tfPost); 
      tf_Ort = (EditText) findViewById(R.id.tfOrt); 
      tf_Strasse = (EditText) findViewById(R.id.tfStrasse); 
      tf_Hausnummer = (EditText) findViewById(R.id.tfHausnummer); 
      spinner_Filter = (Spinner) findViewById(R.id.spinnerFilter); 
      spinner_Pizza = (Spinner) findViewById(R.id.spinnerPizza); 
      spinner_Zusaetzen = (Spinner) findViewById(R.id.spinnerZusaetzen); 
      btn_Senden = (Button) findViewById(R.id.btnSenden); 
      btn_Zuruecksetzen = (Button) findViewById(R.id.btnZuruecksetzen); 

      SPFilterSelectedLine(); 
     } 

     //Determine the chosen position of the filter spinner, convert its text to a string and pass it along: 
     private void SPFilterSelectedLine() { 
      //implement XML file "strings.xml" found in "app\src\main\res\values" 
      //ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.filter,android.R.layout.simple_spinner_item); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.filter)); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinner_Filter.setAdapter(adapter); 

      //Get the position of the chosen element, convert it to text and pass it along 
      spinner_Filter.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
        if (!(view instanceof TextView)) 
         return; 
        TextView item = (TextView) view; 
        String TypeOfPizza = item.getText().toString(); 
       } 

       @Override 
       public void onNothingSelected(AdapterView<?> parent) { 
       } 
      }); 
     } 
    } 

Die Datei strings.xml, die ich für den ArrayAdapter verwende, lautet wie folgt:

 <resources> 
     <string name="app_name">Pizza Delivery App</string> 

     <string-array name="filter"> 
      <item>vegetarische Pizzen</item> 
      <item>fischhaltige Pizzen</item> 
      <item>fleischhaltige Pizzen</item> 
      <item>fischhaltige und fleischhaltige Pizzen</item> 
     </string-array> 

     <string-array name="vegetarisch"> 
      <item>vegetarische Pizza 1</item> 
      <item>vegetarische Pizza 2</item> 
      <item>vegetarische Pizza 3</item> 
      <item>vegetarische Pizza 4</item> 
     </string-array> 

     <string-array name="fischhaltig"> 
      <item>fischhaltige Pizza 1</item> 
      <item>fischhaltige Pizza 2</item> 
      <item>fischhaltige Pizza 3</item> 
      <item>fischhaltige Pizza 4</item> 
     </string-array> 

     <string-array name="fleischhaltig"> 
      <item>fleischhaltige Pizza 1</item> 
      <item>fleischhaltige Pizza 2</item> 
      <item>fleischhaltige Pizza 3</item> 
      <item>fleischhaltige Pizza 4</item> 
     </string-array> 

     <string-array name="fischhaltig_und_fleischhaltig"> 
      <item>fischhaltige und fleischhaltige Pizza 1</item> 
      <item>fischhaltige und fleischhaltige Pizza 2</item> 
      <item>fischhaltige und fleischhaltige Pizza 3</item> 
      <item>fischhaltige und fleischhaltige Pizza 4</item> 
     </string-array> 
    </resources> 

Ich werde dankbar sein, wenn mir jemand helfen kann herauszufinden, was das Problem zu sein scheint.

+0

Ich habe versucht, mit Ihrem XML - es enthält fehlende IDs. Bitte aktualisieren Sie die Frage. Das Hauptproblem scheint zu sein, dass Sie 'android: layout_alignBottom' anstelle von' android: layout_below' verwenden. –

+0

Was und wo ist "ET_Name"? Es gibt keine Definition davon und jedes Element ist in Bezug auf dasselbe ausgerichtet. –

+0

Haben Sie ein Mockup-Bild, das zeigt, wie es sein soll? –

Antwort

0

Ich habe versucht, Ihren XML-Code. Ich änderte Ihr Wurzelelement in ScrollView von RelativeLayout zu LinearLayout mit orientationvertical und es sah gut aus. Sie müssen einige Eigenschaften entfernen.

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:orientation="vertical" 
     tools:context="fadi_christoph.pizzadeliveryapp.MainActivity"> 

     ... 


    </LinearLayout> 

</ScrollView> 
+1

@K Neeraj Lal Ich habe das gesamte Programm wieder von 0 erstellt und deinen Ratschlag benutzt. Es funktioniert jetzt perfekt. Vielen Dank :) – REDX16385

0

Sie sollten versuchen, Höhe von einem anderen Ort zu bearbeiten. Manchmal gibt Android Studio mir auch den Fehler für eine verpasste \ vor ', die einzige Möglichkeit, dass ich weg von dieser Art von Fehler bleiben ist, bearbeiten Sie es von einem normalen Leser wie Block Notizen.

Verwandte Themen