如何阻止RelativeLayout自动更改其高度?

时间:2016-12-18 11:24:45

标签: android android-studio

我正在尝试创建一个应用程序,它在Android Studio的帮助下在ScrollView中有一个RelativeLayout。 我添加了所有需要的按钮,文本字段和微调器,使应用程序看起来就像我希望它看起来一样。 但是,在调试应用程序后,RelativeLayout会自动更改其高度,使按钮,文本字段和旋转器无法使用。

这是调试后应用的图片: App looks messed up

XML文件如下:

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

我的程序的部分代码如下:

    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) {
                }
            });
        }
    }

我用于ArrayAdapter的strings.xml文件如下:

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

如果有人能帮我弄清问题是什么,我会很高兴。

2 个答案:

答案 0 :(得分:0)

我尝试了你的xml代码。我使用ScrollView RelativeLayoutLinearLayout中的根元素从orientation更改为vertical,看起来很不错。您必须删除一些RelativeLayout属性。

<?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 :(得分:0)

您应尝试从其他位置编辑高度。有时Android Studio也会给我错误的错误\'之前',我想出的远离这种类型错误的唯一方法是从正常的读者中编辑它,比如块注释。