调用后,TextView边框颜色未设置为黄色

时间:2019-02-09 14:17:02

标签: android xml kotlin

我正在学习Android,并在名为PlayActivity的Activity中创建了具有两行的TableLayout。在每一行中,我都希望使用Edge中带有后缀colors.xml的颜色带有边框的TextView。一切正常,除了YELLOW(文本)颜色不会导致tv.setBackgroundResource(R.drawable.yellow_edge)中的黄色边框颜色。

我正在制作一个游戏,其中用户最终进入PlayActivity,并从“包”中绘制了许多图块。这些图块的颜色以大写字母表示(例如YELLOW),而边框在colors.xml文件中定义为另一种颜色。我检查了yellow_edge的颜色是否不透明。我不明白为什么其他颜色起作用而另一种颜色不起作用。谁能帮忙找到我的错误?我得到的输出是这样的: output

我的图块课程:

import android.graphics.Color.rgb
val BLUE: Int = rgb(0, 0, 255)
val RED: Int = rgb(255, 0, 0)
val YELLOW: Int = rgb(255,235,0)
val BLACK: Int = rgb(0, 0, 0)


class Tile(val col: Int = 0, val num: Int = 0){
    val name = "$col $num"
}

我如何实例化我的图块:

class PlayActivity : AppCompatActivity() {
    val COLOURS = listOf(BLUE, RED, YELLOW, BLACK)
    val NUMBERS = IntRange(1, 13)
    lateinit var nameTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_play)
        val pack = makeTiles()
        nameTv = findViewById(R.id.nameTextView)
        val player = Player("Jack", plankGrid, this)
        nameTv.text = player.name
        player.drawTiles(14, pack)

    }
    private fun makeTiles(): MutableList<Tile>{
        val a = mutableListOf<Tile>()
        var count = 0
        for (value in COLOURS) {
            for (number in NUMBERS) {
                count += 1
                val tile = Tile(value, number)
                a.add(tile)
            }
        }
        a.shuffle()
        return a
    }
}

activity_play.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context=".PlayActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/app_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp" android:id="@+id/textView"/>

    <TableLayout
            android:layout_width="440dp"
            android:layout_height="100dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintVertical_bias="1.0" android:orientation="horizontal" android:gravity="top|start"
            android:id="@+id/plankGrid">

        <TableRow android:id="@+id/row1" android:layout_width="match_parent" android:layout_height="match_parent"
                  android:orientation="horizontal"/>
        <TableRow android:id="@+id/row2" android:layout_width="match_parent" android:layout_height="match_parent"
                  android:orientation="horizontal"/>
    </TableLayout>


</android.support.constraint.ConstraintLayout>

我的课中用TextViews填充TableLayout:

import android.app.Activity
import android.view.Gravity
import android.view.View
import android.widget.TableLayout
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_play.*
import kotlinx.android.synthetic.main.activity_play.view.*


class Player(val name: String = "Jack",
             private var table: TableLayout,
             private val ct: Activity,
             private var count: Int = 0) {

    fun drawTiles(n: Int, pack: MutableList<Tile>) {
        for (i in IntRange(0, n-1)) {
            addTileToTable(pack[i])
            count += 1
        }
    }

    fun addTileToTable(tile: Tile) {
        val tv = TextView(ct)
        tv.setTextColor(tile.col)
        tv.textSize = 24.toFloat()
        tv.text =  tile.num.toString()
        tv.textAlignment = View.TEXT_ALIGNMENT_GRAVITY
        tv.gravity = Gravity.CENTER
        tv.width = table.layoutParams.width / 15
        tv.height = table.layoutParams.height / 2
        when (tile.col) {  // check colours and then set the border around the textview to that colour
            BLUE -> tv.setBackgroundResource(R.drawable.blue_edge)
            RED -> tv.setBackgroundResource(R.drawable.red_edge)
            YELLOW -> tv.setBackgroundResource(R.drawable.yellow_edge) // this is where it apparently goes wrong
            BLACK -> tv.setBackgroundResource(R.drawable.black_edge)
        }
        if (count <= 15) {
            table.row1.addView(tv)
        }
        if (count in 16..30) {
            table.row2.addView(tv)
        }
    }
}

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="bluebck">#2882D1</color>
    <color name="blueEdge">#004780</color>
    <color name="white">#ffffff</color>
    <color name="redEdge">#E70000</color>
    <color name="yellowEdge">#FFEB3B</color>
    <color name="blackEdge">#000000</color>
</resources>

yellow_edge.xml,为了比较也blue_edge.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="1dp"/>
            <stroke android:width="1dip" android:color="@color/yellowEdge"/>
            <padding android:left="1dp" android:right="1dp"/>
        </shape>
    </item>
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="1dp"/>
            <stroke android:width="1dip" android:color="@color/blueEdge"/>
            <padding android:left="1dp" android:right="1dp"/>
        </shape>
    </item>
</selector>

1 个答案:

答案 0 :(得分:1)

好的,在使用您提供的代码创建新项目之后,我发现了以下内容:

您将颜色定义为常量,而不必在ActivityPlayer类中导入颜色。

现在,我的假设是,在其中一个类中,您导入YELLOW错误。您自己定义了此颜色,但是 import android.graphics.Color.YELLOW 也存在。我相信您错误地导入了此内容,而不仅仅是import YELLOW