【问题标题】:ImageView appears in the wrong place in LinearLayoutImageView 在 LinearLayout 中出现错误的地方
【发布时间】:2015-01-30 02:54:25
【问题描述】:

我正在学习 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>

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

至少教程是这样显示的。

我尝试将 ImageView 部分放在 TextViews 之上,然后我根本没有得到任何文本,并且图像仍在中间。

自从这本书出版以来,行为发生了变化还是我遗漏了什么?

【问题讨论】:

  • 这是您在设计视图或实际应用中看到的内容吗?
  • test_image 的高度是多少?
  • 在注释掉 ImageView 后看看你是否得到了正确的行为。如果您看到文本以正确的顺序出现,那么您的 ImageView 有问题。你能发布“@drawable/test_image”的大小和类型吗?
  • 尝试将ImageView 的大小设置为56dp*56dp 之类的小值,或者在&lt;LinearLayout&gt; 周围放置一个&lt;ScrollView&gt;。有些东西告诉我图片太大了。
  • 图片尺寸为1000x773。

标签: android textview imageview android-linearlayout


【解决方案1】:

我找到了答案:ImageView in android XML layout with layout_height="wrap_content" has padding top & bottom

你需要把它添加到 ImageView 中:

android:adjustViewBounds="true"

由于某种原因 wrap_content 不适用于没有那条线的高度。

【讨论】:

    【解决方案2】:

    如果您不知道文本多长或无法指定图像高度和重量,您可以使用wrap_content,但您必须在LinearLayout 上定义ImageViewTextView 位置。例如,您可以使用android:paddingLeftandroid:paddingTopandroid:layout_centerHorizontalandroid:layout_alignParentTop 等定义ImageView 位置。这就是我在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

    <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"/>
    

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

    【讨论】:

      【解决方案3】:

      正如您上面的问题,如果您的布局像您的示例中那样正确定义,它将显示 4 行,并且您的组件显示每行。尝试将 android:orientation="vertical" 更改为 horizo​​ntal 以显示完整的组件填充每一行,如果我误解了请见谅

      【讨论】:

      • 不,他希望视图垂直对齐。在我看来是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多