2015-09-28 51 views
1

我在编写Android游戏。在级别评选活动的布局文件,我想布局水平按钮(他们实际上是ImageView S)是这样的:为什么我的TextView下的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。但是,当我看设计师,文本视图不显示件事。当我在组件树中选择文本视图,它显示了文本的看法是:

enter image description here

附:图片没有显示所有六个级别,因为我还没有完成。

正如你所看到的,文本视图正好在底部!当我选择ImageView时,它显示它占据了其父项的所有空间!

enter image description here

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

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

+0

您需要使用RelativeLayout而不是LinearLayout。 LinearLayout不允许2个或更多对象占据相同的空间。如果你想要发生这种情况,你需要使用RelativeLayout。 – Rachit

+0

平面布局?为什么不是相对布局,它给了你更多的灵活选项 –

+0

使用RelativeLayout,并将ImageView的顶部对齐到TextView的顶部,你会看到两个视图占用相同的空间。 – Rachit

回答

1

对我来说,最好的解决办法是不是XML来定位,并通过代码(如果你有总量控制),它的大小正常。

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

imageView1.setScaleType(ScaleType.FIT_START); 

通过XML来解决:

android:scaleType="fit_start" 

希望这有助于。

0

我在学习布局时使用textview的背景颜色。

如果在两个维度中为TextView使用包装内容,由于您没有在其中编写任何文本,因此它是不可见的。包装内容意味着视图占用最小的空间。没有文字表示0px;尝试使用layout_weight 1和layout_height 0dp设置ImageView和TextView。通过这种方式,这两种视图都占用父级布局的一半空间

0

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

我才意识到,我发布了一个问题关于ImageView要走出太多空白:

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

可以使用相对布局

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

设置的TextView的背景,该图像
相关问题