为什么我的 TextView 在 ImageView 下没有显示
Why my TextView under an ImageView is not showing
我正在编写一个 Android 游戏。在级别 selection activity 的布局文件中,我想像这样布局级别的按钮(它们实际上是 ImageView
s):
x x x
x x x
并且每个级别按钮都有一个 TextView
,该级别的名称作为文本位于其下方(让我们将这两个视图统称为 "level choice")。我用了很多 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>
可以看到,两个view的高和宽都是wrap_content
。但是当我查看设计器时,文本视图不显示 up.When 我 select 组件树中的文本视图,它显示文本视图在哪里:
P.S。图片没有显示所有六个级别,因为我还没有制作它们。
如您所见,文本视图就在底部!当我select the ImageView
时,说明它占用了它parent的所有space!
我不知道为什么会这样,我的图像肯定是正方形!你能解释为什么会这样吗?我该如何解决?
如果您需要我的整个布局代码,请随时在评论中告诉我。
我在研究布局的时候用textview的背景色。
如果您在 TextView 的两个维度中使用 wrap content,那是不可见的,因为您没有在其中写入任何文本。 wrap content 表示视图取最小space。没有文本意味着 0px;尝试将 ImageView 和 TextView 设置为 layout_weight 1 和 layout_height 0dp。这样,两个视图都占父布局
的 space 的一半
因为现在,您的 LinearLayout 不知道如何分配其 children 的比例。事实上,您的 imageview 的包装内容已经
消耗整个 space.
因此,LinearLayout 表示 "Sorry TextView, you have no space left"。
对children都使用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>
对我来说,最好的解决方案是通过代码(您可以完全控制)而不是 xml.
正确定位和调整大小
无论如何,我认为您的问题可以通过设置 ImageViews ScaleType 来解决
imageView1.setScaleType(ScaleType.FIT_START);
来自 XML:
android:scaleType="fit_start"
希望对您有所帮助。
我刚刚意识到我发布了一个关于 ImageView
s 遗漏了太多空格的问题:
我觉得这个和那个问题是一样的。所以我尝试在 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>
或者简单地说,您可以通过将此
设置为该图像的 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"/>
我正在编写一个 Android 游戏。在级别 selection activity 的布局文件中,我想像这样布局级别的按钮(它们实际上是 ImageView
s):
x x x
x x x
并且每个级别按钮都有一个 TextView
,该级别的名称作为文本位于其下方(让我们将这两个视图统称为 "level choice")。我用了很多 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>
可以看到,两个view的高和宽都是wrap_content
。但是当我查看设计器时,文本视图不显示 up.When 我 select 组件树中的文本视图,它显示文本视图在哪里:
P.S。图片没有显示所有六个级别,因为我还没有制作它们。
如您所见,文本视图就在底部!当我select the ImageView
时,说明它占用了它parent的所有space!
我不知道为什么会这样,我的图像肯定是正方形!你能解释为什么会这样吗?我该如何解决?
如果您需要我的整个布局代码,请随时在评论中告诉我。
我在研究布局的时候用textview的背景色。
如果您在 TextView 的两个维度中使用 wrap content,那是不可见的,因为您没有在其中写入任何文本。 wrap content 表示视图取最小space。没有文本意味着 0px;尝试将 ImageView 和 TextView 设置为 layout_weight 1 和 layout_height 0dp。这样,两个视图都占父布局
的 space 的一半因为现在,您的 LinearLayout 不知道如何分配其 children 的比例。事实上,您的 imageview 的包装内容已经 消耗整个 space.
因此,LinearLayout 表示 "Sorry TextView, you have no space left"。
对children都使用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>
对我来说,最好的解决方案是通过代码(您可以完全控制)而不是 xml.
正确定位和调整大小无论如何,我认为您的问题可以通过设置 ImageViews ScaleType 来解决
imageView1.setScaleType(ScaleType.FIT_START);
来自 XML:
android:scaleType="fit_start"
希望对您有所帮助。
我刚刚意识到我发布了一个关于 ImageView
s 遗漏了太多空格的问题:
我觉得这个和那个问题是一样的。所以我尝试在 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>
或者简单地说,您可以通过将此
设置为该图像的 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"/>