Android ImageView显示在LinearLayout中的错误位置

Android ImageView显示在LinearLayout中的错误位置,android,textview,imageview,android-linearlayout,Android,Textview,Imageview,Android Linearlayout,我正在遵循Head First Android Development的教程,我不明白为什么LinearLayout不能正常工作 以下是xml代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:orientation="

我正在遵循Head First Android Development的教程,我不明白为什么LinearLayout不能正常工作

以下是xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/imageTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_image_title"/>

<TextView
    android:id="@+id/imageDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_image_date"/>

<ImageView
    android:id="@+id/imageDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/test_image"/>

<TextView
    android:id="@+id/imageDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_image_description"/>

</LinearLayout>

问题是图像在中间,但是图像应该正好在上边显示的两行文字下面,而不是在中间。最后一个文本视图也没有显示。所有内容都应该按照xml中声明的顺序显示

最后,这就是教程显示的内容

我试着把IVIEVIEW部分放在文本视图的顶部,然后我根本没有文本,图像仍然在中间。


现在,自本书发行以来,行为发生了变化,还是我遗漏了什么?

正如您上面的问题,如果您的布局定义与示例中一样正确,它将显示4行,并且您的组件将显示每行。尝试将android:orientation=“垂直”更改为水平,以显示每行的完整组件填充,如果我误解了,很抱歉我从以下位置找到了答案:

您需要将其添加到ImageView:

android:adjustViewBounds="true"

由于某些原因,如果没有该行,包裹内容不适用于高度。

如果您不知道文本的长度或无法指定图像的高度和重量,您可以使用
包裹内容
,但必须定义
图像视图
文本视图
线性布局上的位置。例如,您可以使用以下命令定义
ImageView
位置:
android:paddingLeft
android:paddingTop
android:layout\u centerHorizontal
android:layoualignparenttop
,等等。这就是我在
ImageView
上使用该代码的方式:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="88dp"
    android:src="@drawable/your_image_resource" />
<TextView
    android:id="@+id/textView1"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textSize="16dp"
    android:text="My TextView1"/>

你可以在谷歌上找到另一个
android:[position]
属性以及如何使用它。

这是你在设计视图或实际应用程序中看到的吗?以像素为单位的测试图像的高度是多少?查看你在注释完ImageView后是否得到正确的行为。如果您看到文本以正确的顺序显示,则说明ImageView有问题。您可以发布“@drawable/test_image”的大小和类型吗?请尝试将
ImageView
大小设置为56dp*56dp之类的小值,或者在
周围放置一个
。有人告诉我图像太大了。图像大小是1000x773。不,他希望视图垂直对齐。在我看来这是正确的。