android - 为什么我的文本和按钮不显示并被我的图像覆盖?
问题描述
为什么我的文字和按钮不显示?当我删除 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>
解决方案
更新
关于模拟器和 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>
推荐阅读
- multithreading - 为什么 QCoreApplication::processEvents() 会阻塞 QThread::wait()?
- react-native - 检查元素中的 React-Native 白色区域
- db2 - 如何使用 Jupyter Notebook 连接到 db2 而不会遇到 sql 错误
- apache-spark - Pyspark:检查每个单元格的条件并计算校验位
- sql - 左连接与内连接 ksqldb
- javascript - javascrpt中的规模平衡算法
- android - 无法为自定义 buildVariant 创建 androidTest 目录(缺少 androidTest 目录)
- neo4j - 如何在 Neo4j 中导入 .OWL 文件?
- windows - 从批处理 zip 中排除根文件夹
- powershell - 改进 PowerShell 功能