首页 > 解决方案 > 为什么我的文本和按钮不显示并被我的图像覆盖?

问题描述

为什么我的文字和按钮不显示?当我删除 ImageView 时出现文本和按钮。我已经尝试过 LinearLayout 和 RelativeLayout。它们都不起作用。这是我的代码。线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:src="@drawable/before_cookie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/before_eating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="18dp"
        android:textSize="36dp"
        android:text="I'm so hungry!"
        android:textColor="@color/black"
        android:textStyle="bold" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36dp"
        android:text="Eat Cokkie"
        android:onClick="eating" />
</LinearLayout>

线性布局的结果 在此处输入图像描述

相对布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/before_cookie"
        android:src="@drawable/before_cookie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/before_eating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/before_cookie"
        android:layout_margin="18dp"
        android:textSize="36dp"
        android:text="I'm so hungry!"
        android:textColor="@color/black"
        android:textStyle="bold" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/before_eating"
        android:textSize="36dp"
        android:text="Eat Cokkie"
        android:onClick="eating" />
</RelativeLayout>

相对布局的结果 在此处输入图像描述

左模拟器与右预览不同。 在此处输入图像描述

标签: androidkotlin

解决方案


更新

关于模拟器和 Android Studio 显示不同的视图,原因是您的模拟器屏幕尺寸为 4.7 英寸屏幕,而您在 Android Studio 中用于预览的像素 3 具有 5.5 英寸屏幕。

:::::::::::::::::

这发生在您身上的原因是您选择wrap_content高度宽度意味着对于大图像,您的图像视图将占用更多区域来包装内容而不改变纵横比。

您针对较小图像的相同代码将显示您缺少的按钮和 texview。

所以如果你想要你的图像,wrap_content你应该在滚动视图中添加你的布局,否则你可以为你的ImageView.

  <ScrollView 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"
    android:fillViewport="true">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:src="@drawable/before_cookie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
      />
    <TextView
        android:id="@+id/before_eating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="18dp"
        android:textSize="36dp"
        android:text="I'm so hungry!"
        android:textColor="@color/black"
        android:textStyle="bold" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36dp"
        android:text="Eat Cokkie"
        android:onClick="eating" />
</LinearLayout>

    </ScrollView>

推荐阅读