为什么我的 ImageView 显示在顶部?



我对安卓很陌生,一直在关注相机应用程序的简单教程。它在FrameLayout(朝向顶部)中显示相机的预览,然后在点击FrameLayout正下方的按钮后捕获图像。然后,图像将显示在应该位于按钮下方的 ImageView 中。但是,当我运行程序并单击按钮拍照时,捕获的图像出现在FrameLayout的顶部。我尝试在 activity_main.xml 的设计选项卡中解决此问题,但我似乎无法在设计中找到 ImageView,即使它列在我的组件树中。

以下是我的 xml 文件中的三个组件:

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera_preview"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/capture_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text"
app:layout_constraintVertical_bias="0.143" />
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="300dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/captured_image"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:contentDescription="@string/desc" />

我能做些什么来解决这个问题吗?

如果您希望图像视图显示在按钮和框架布局下方

将其添加到您的框架布局标签上

android:layout_below="@id/button"

您编辑的代码。

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera_preview"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/capture_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text"
app:layout_constraintVertical_bias="0.143" />
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@id/button"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/captured_image"
android:layout_below="@+id/camera_preview"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:contentDescription="@string/desc" />

干杯!

最新更新