Android Studio预览和模拟器显示出很大差异

时间:2018-08-09 14:22:53

标签: android layout emulation

我是android编程的新手。

我在android studio中创建了一个新视图,但模拟器没有像预览一样显示它。我不知道是什么原因。

Preview in Android Studio

Emulator

ConstraintLayout是默认的。是否可以使用另一个?为什么我所有的元素都在视图的顶部?属性丢失了吗?

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".activity.DetailDeviceInformation">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:layout_weight="0.11"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="147dp">

    <TextView
        android:id="@+id/weight_label"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.18"
        android:text="@string/gewicht"
        android:textAlignment="gravity"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/weightDropdown"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_weight="0.00" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:layout_weight="0.07"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="204dp">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="4.26"
        android:text="@string/wiederholungen"
        android:textAlignment="gravity"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/repetitionDropdown"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_weight="0.07" />

</LinearLayout>

<Button
    android:id="@+id/saveButton"
    android:layout_width="match_parent"
    android:layout_height="62dp"
    android:layout_weight="0.07"
    android:text="@string/button"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="418dp"
    tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>

4 个答案:

答案 0 :(得分:1)

您将约束布局用作基本布局。但是该声明丢失了。

因此在顶部添加以下代码

<android.support.constraint.ConstraintLayout 
    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">

我发现您正在使用textView和spinner并将这两个包装成线性布局。但您无需使用线性布局。您应该删除这两个多余的线性布局。

答案 1 :(得分:0)

尝试使用这种方式realtive layout

更改:

<android.support.constraint.ConstraintLayout 
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=".activity.DetailDeviceInformation">



</android.support.constraint.ConstraintLayout>

至:

<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"
tools:context=".activity.DetailDeviceInformation"
>

</RelativeLayout>

答案 2 :(得分:0)

按如下所示重构您的代码,以填充微调框,请遵循以下文档:https://developer.android.com/guide/topics/ui/controls/spinner

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content">

    <Spinner
        android:id="@+id/weightDropdown"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/repetitionDropdown" />

    <Spinner
        android:id="@+id/repetitionDropdown"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/saveButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weightDropdown"
        tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>

答案 3 :(得分:0)

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="147dp"
        android:layout_weight="0.11"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/weight_label"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.18"
            android:text="sss"
            android:textAlignment="gravity"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/weightDropdown"
            android:layout_width="190dp"
            android:layout_height="match_parent"
            android:layout_weight="0.00" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:layout_marginBottom="32dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.07"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/saveButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4.26"
            android:text="ssss"
            android:textAlignment="gravity"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/repetitionDropdown"
            android:layout_width="190dp"
            android:layout_height="match_parent"
            android:layout_weight="0.07" />

    </LinearLayout>

    <Button
        android:id="@+id/saveButton"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="84dp"
        android:layout_weight="0.07"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
        tools:ignore="MissingConstraints"
        tools:text="Speichern" />

</android.support.constraint.ConstraintLayout>