Android 调用TextView边框颜色后未将其设置为黄色

Android 调用TextView边框颜色后未将其设置为黄色,android,xml,kotlin,Android,Xml,Kotlin,我正在学习Android,在一个名为PlayActivity的活动中创建了一个两行的表格布局。在每一行中,我都放置了一些文本视图,这些文本视图的边框是colors.xml中给定的带有后缀Edge的颜色。除了在tv.setBackgroundResource(R.drawable.YELLOW\u edge)中,黄色(文本)颜色不会导致黄色边框外,一切正常 我正在制作一个游戏,其中用户在PlayActivity中结束,并从一个“包”中绘制一些瓷砖。这些瓷砖具有大写颜色的数字(例如,黄色),以及co

我正在学习Android,在一个名为PlayActivity的活动中创建了一个两行的表格布局。在每一行中,我都放置了一些文本视图,这些文本视图的边框是
colors.xml
中给定的带有后缀
Edge
的颜色。除了在
tv.setBackgroundResource(R.drawable.YELLOW\u edge)
中,黄色(文本)颜色不会导致黄色边框外,一切正常

我正在制作一个游戏,其中用户在PlayActivity中结束,并从一个“包”中绘制一些瓷砖。这些瓷砖具有大写颜色的数字(例如,
黄色
),以及
colors.xml
文件中定义的另一种颜色的边框。我检查了我的黄色边缘颜色是否不透明,它是否透明。我不明白为什么其他颜色能用,而这一种不行。谁能帮我找出我的错误吗?我得到的输出是:

我的瓷砖类:

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
    }
}
在我的类中,我用文本视图填充表格布局:

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)
        }
    }
}
yellow\u edge.xml
以及用于比较的
blue\u edge.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>
<?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>

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

您将颜色定义为常量,必须在
Activity
Player
类中导入这些常量


现在我的假设是,在其中一个类中,您导入的
YELLOW
是错误的。您自己定义了此颜色,但导入android.graphics.color.YELLOW也存在。我认为您错误地导入了此文件,而不是仅导入黄色文件

您确定title.col是黄色的吗?你的黄色边缘看起来怎么样?你能展示一下
瓷砖
类吗?@ReneFerrari谢谢你的建议。我刚用调试器检查过,结果显示tile.col是-8448,而黄色是-5376。但是,它们也与
when
块中的其他行不匹配。我会用黄色边缘更新这个问题。非常感谢你花时间来了解这个问题!我尝试了您的解决方案,在这样做的过程中,我找到了错误的真正来源。事实证明,导入并不是问题所在(我根本没有导入颜色,而是使用了全局变量),但我意外地在PlayActivity类中重新定义了颜色,并且在
Player
类中的
when
语句中没有识别出黄色。至少现在是这样。再次感谢您的时间:)很高兴它仍然有用!我真的以为我已经找到了解决办法呵呵;P
<?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>