无法在GridLayout Android中更改ImageView背景

时间:2016-06-29 04:18:38

标签: android

我正在使用带有xml布局的gridlayout,如下所示:

<?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永远不会改变。那么这里的问题是什么......?我在网格布局中遗漏了什么...? 提前谢谢。

4 个答案:

答案 0 :(得分:0)

而不是为textviews指定背景,而是将src属性设置为透明图像,将背景设置为其中一个图像视图所需的背景颜色?

答案 1 :(得分: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"
            />

答案 2 :(得分: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"
            />

答案 3 :(得分:0)

为什么不直接将背景设置为Textview。使用ImageView为TextView设置背景的目的是什么。

将背景设置为Textview并检查它是否解决了您的问题