2015-09-28 54 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的背景,該圖像
相關問題