【问题标题】:Why my TextView under an ImageView is not showing为什么我在 ImageView 下的 TextView 没有显示
【发布时间】:2015-09-28 15:44:27
【问题描述】:

我正在编写一个 Android 游戏。在关卡选择活动的布局文件中,我想像这样布局关卡的按钮(它们实际上是ImageViews):

x x x
x x x

每个级别按钮都有一个TextView,在其下方以该级别的名称作为文本(我们将这两个视图一起称为“级别选择”)。我使用了很多LinearLayouts 来做到这一点。以下是关卡选择的代码:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_weight="1">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/angles"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/angles_level"
        android:textSize="@dimen/level_text_size"/>
</LinearLayout>

如您所见,两个视图的高度和宽度都是wrap_content。但是当我查看设计器时,文本视图没有显示出来。当我在组件树中选择文本视图时,它显示了文本视图的位置:

附:图片没有显示所有六个级别,因为我还没有制作它们。

如您所见,文本视图位于底部!当我选择ImageView时,它表明它正在占用其父级的所有空间!

我不知道为什么会这样,我的图像肯定是一个正方形!你能解释一下为什么会发生这种情况,我该如何解决?

如果您需要我的整个布局代码,请随时在 cmets 中告诉我。

【问题讨论】:

  • 您需要使用RelativeLayout 而不是LinearLayout。 LinearLayout 不允许 2 个或更多对象占用相同的空间。如果你想让这种情况发生,你需要使用RelativeLayout。
  • 线性布局?为什么不相对布局它为您提供更灵活的选择
  • 使用RelativeLayout,将ImageView的顶部对齐到TextView的顶部,你会看到两个视图占据了相同的空间。
  • 您可以做一件事来检查您的文本区域是否存在于视图中,只需在文本视图中添加背景颜色并检查是否可见?
  • 使用包含行和列的表格布局

标签: android android-layout imageview android-xml


【解决方案1】:

对我来说,最好的解决方案是通过代码(您可以完全控制)而不是 xml 来正确定位和调整大小。

无论如何,我认为你的问题可以通过设置 ImageViews ScaleType 来解决

imageView1.setScaleType(ScaleType.FIT_START);

通过 XML:

  android:scaleType="fit_start"

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我在研究布局时使用 textview 的背景颜色。

    如果您在 TextView 的两个维度中使用包装内容,那是不可见的,因为您没有在其中写入任何文本。包装内容意味着视图占用最小空间。没有文字意味着0px;尝试使用 layout_weight 1 和 layout_height 0dp 设置 ImageView 和 TextView。这样,两个视图都占用了父布局的一半空间

    【讨论】:

      【解决方案3】:

      因为现在,您的 LinearLayout 不知道如何分配其子级的比例。事实上,你的 imageview 的包装内容已经 占用整个空间。

      所以,LinearLayout 说“对不起,TextView,你没有空间了”。

      对两个孩子都使用layout_weight。 我猜你想让你的图片两倍于你的文字大小。 2:1

      也就是说,

      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="1">
          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight=2
            android:src="@drawable/angles"/>
          <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight=1
            android:text="@string/angles_level"
            android:textSize="@dimen/level_text_size"/>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        我刚刚意识到我发布了一个关于ImageViews 遗漏了太多空格的问题:

        LinearLayout leaving out too much white space. Why?

        我认为这与那个问题相同。所以我尝试在xml中将adjustViewBounds设置为true。它有效!现在图像视图如下所示:

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/parallel_lines"/>
        

        【讨论】:

          【解决方案5】:

          你可以使用相对布局

              <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal">
          
                 <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/angles"/>
                 <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/angles_level"
                    android:textSize="@dimen/level_text_size"/>
          </RelativeLayout>
          

          或者很简单,您可以通过将这个设置为该图像的 textview 背景

          <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"
          >
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/angles_level"
              android:background="@drawable/angles"
              android:textSize="@dimen/level_text_size"/>
          

          【讨论】:

            猜你喜欢
            • 2013-05-06
            • 2021-10-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多