1

我必须用按钮和图像制作一个基本的主屏幕。我之前在提取字符串资源时遇到了麻烦,但我想我已经弄清楚了,希望这不会干扰代码。我什至不需要按钮去任何地方,我只需要它们出现。我的代码如下所示:

<Button  
    android:id="@+id/button4"
    android:layout_width="139dp"
    android:layout_height="74dp"
    android:text="@string/my_cookbook"

    app:layout_constraintStart_toEndOf="@+id/button2"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<Button
    android:id="@+id/button2"
    android:layout_width="127dp"
    android:layout_height="0dp"
    android:layout_marginBottom="359dp"


    android:text="@string/recipes"
    app:layout_constraintBottom_toTopOf="@+id/button3"
    app:layout_constraintEnd_toStartOf="@+id/button4"
    app:layout_constraintStart_toEndOf="@+id/imageView4"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="39dp"
    android:text="@string/check_fridge"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button2" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="59dp"
    android:layout_height="80dp"
    android:layout_marginBottom="108dp"
    android:contentDescription="@string/heart"
    app:layout_constraintBottom_toTopOf="@+id/imageView3"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintStart_toEndOf="@+id/imageView5"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/heart" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="110dp"
    android:layout_marginStart="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:srcCompat="@drawable/fridge"
    android:contentDescription="@string/to_do"
    android:layout_marginLeft="20dp" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="31dp"
    android:layout_height="0dp"
    android:layout_marginBottom="134dp"
    app:layout_constraintBottom_toTopOf="@+id/imageView3"
    app:layout_constraintEnd_toStartOf="@+id/imageView4"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/loaf"
    android:contentDescription="@string/to_do2" />

而且我没有收到任何错误,这表示我的构建成功,但我无法让应用程序在我的 Galaxy S4 上运行。模拟器不适合我,所以我决定用我的手机运行它,但它会弹出一秒钟然后说“应用程序已停止工作”。知道为什么它实际上不会运行吗?

4

1 回答 1

0

If it is the complete XML layout file, then you're missing the "container" of the views, until you add some "container" like LinearLayout or RelativeLayout and then put the views/buttons/imageviews inside that container, it wont show anything. IF it is the complete XML code, also you should try adding a bit more code.

于 2017-11-17T17:04:20.253 回答