2016-06-29 59 views
0

我使用GridLayout的XML佈局是這樣的:不能在網格佈局更改Android ImageView的背景

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:columnCount="8" 
     app:rowCount="8" 
     > 
<ImageView 
     android:id="@+id/papan8a" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:background="@drawable/white_queen" 
     app:layout_row="0" 
     app:layout_column="0" 
     /> 

    <TextView 
     android:id="@+id/bidak8a" 
     android:background="@drawable/black_rook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Contoh" 
     app:layout_row="0" 
     app:layout_column="0" 
     /> 

    <ImageView 
     android:id="@+id/papan8b" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:background="@color/kuningDikit" 
     app:layout_row="0" 
     app:layout_column="1" 
     app:layout_columnWeight="0.5" 
     /> 

    <TextView 
     android:id="@+id/bidak8b" 
     android:background="@drawable/black_rook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Contoh" 
     app:layout_row="0" 
     app:layout_column="1" 
     /> 
    </android.support.v7.widget.GridLayout> 
</LinearLayout> 

輸出是這樣的:

enter image description here

的ImageView應該成爲一個黃色背景對於我的textView,但它從未顯示。儘管我把android:background="#FF0000" imageView永遠不會改變。那麼這裏有什麼問題......?我在網格佈局中錯過了什麼......? 在此先感謝。

+0

您確切的要求是什麼? – Prasad

+0

我想使imageView作爲一個飛機和textview上...這就像一個國際象棋遊戲先生@Prasad ... –

+0

爲了您的要求,您可以只使用imageview與背景和src屬性,也檢查您的imageview圖像的大小,它可能是小於textview圖像大小,因此它隱藏在texiview – Vickyexpert

回答

0

我剛剛將android:background更改爲android:src="",現在它變得可見。

<ImageView 
      android:id="@+id/papan8a" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="fitXY"     
      app:layout_row="0" 
      app:layout_column="0" 
      android:src="@drawable/latar_kuning" 
      /> 
0

而不是將背景分配給textviews,而是將src屬性設置爲透明圖像和背景以在其中一個圖像視圖上顯示所需的背景顏色?

+0

Iyaps ...我相信這是一個透明的圖像先生。 Zachary ... :) –

+0

而不是將背景分配給textviews,而是將src屬性設置爲透明圖像和背景以在其中一個圖像視圖上顯示所需的背景顏色? –

+0

ooowh ..所以我必須使用src ... okiee ..讓我試試Zach先生...;) –

0

不使用背景,而是使用src來設置Imageview背景。

希望這將工作

<ImageView 
      android:id="@+id/papan8a" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="fitXY"     
      app:layout_row="0" 
      app:layout_column="0" 
      android:src="@drawable/xxxx" 
      /> 
+0

如果我使用android:src,它只適用於第一次coulumb,但第二次或下一次coulumb,它仍然是不可見的 –

0

你爲什麼不設置背景直接到TextView的。使用ImageView爲TextView設置背景的目的是什麼?

將背景設置爲Textview並檢查它是否可以解決您的問題