java - 尝试运行一个带有按钮的简单应用程序

标签 java android samsung-galaxy

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

<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 上运行。模拟器不适合我,所以我决定使用手机来运行它,但它弹出一秒钟,然后说“应用程序已停止工作”。知道为什么它实际上不会运行吗?

最佳答案

如果它是完整的XML布局文件,那么你就缺少 View 的“容器”,直到你添加一些“容器”,例如LinearLayout或RelativeLayout,然后将 View /按钮/ ImageView 放入该容器中,它不会显示任何内容。如果它是完整的 XML 代码,您还应该尝试添加更多代码。

关于java - 尝试运行一个带有按钮的简单应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353272/

相关文章:

Android adb devices 没有检测到我的手机

Java 存储对象

java - 带顶部 View 的自定义 ServiceView 在 6.5 分钟后导致应用程序/手机崩溃

android - 在普通函数中调用 .enqueue 还是在 kotlin 挂起函数中调用 .execute 更好?

java - 库 com.google.android.gms :play-services-basement is being requested

android - 更新到 Android 7.0 Nougat 后无法在 Samsung Galaxy S7 Edge 上进行调试

javascript - 在 native 三星浏览器中处理锁屏事件

java - 将图像存储在oracle数据库中--错误--ORA-01460 : unimplemented or unreasonable conversion requested

java - JTextField中如何实现符号校验?

java - AccountManager 帐户选择器 Intent