繁体   English   中英

为什么我的ImageView下的TextView没有显示

[英]Why my TextView under an ImageView is not showing

我正在写一个Android游戏。 在关卡选择活动的布局文件中,我要像这样对关卡的按钮(实际上是ImageView )进行布局:

x x x
x x x

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

<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 但是当我查看设计器时,不会显示文本视图,当我在组件树中选择文本视图时,它将显示文本视图的位置:

在此处输入图片说明

PS该图片没有显示所有六个级别,因为我还没有制作。

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

在此处输入图片说明

我不知道为什么会这样,我的形象肯定是正方形! 您能解释一下为什么会发生这种情况以及如何解决吗?

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

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

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

imageView1.setScaleType(ScaleType.FIT_START);

通过XML:

  android:scaleType="fit_start"

希望这可以帮助。

在研究布局时,我将背景颜色用于textview。

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

因为现在,您的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>

我刚刚意识到,我发布了一个有关ImageView的问题,该问题ImageView了太多的空格:

LinearLayout留出太多空白。 为什么?

我认为这与该问题相同。 所以我尝试在xml adjustViewBounds设置为true。 而且有效! 现在,图像视图如下所示:

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

您可以使用相对布局

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

或简单,您可以通过将文本视图的背景设置为该图像

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM