【问题标题】:TextView border colour not set to yellow after calling it调用后TextView边框颜色未设置为黄色
【发布时间】:2019-02-09 14:17:02
【问题描述】:

我正在学习 Android,并在名为 PlayActivity 的 Activity 中创建了一个包含两行的 TableLayout。在每一行中,我将我想要在颜色中添加边框的 TextViews 放在colors.xml 中给出的后缀Edge 中。一切正常,除了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>

【问题讨论】:

  • 你确定 title.col 是黄色的吗?你的 yellow_edge 怎么样?
  • 你能不能也显示Tile类?
  • @ReneFerrari 感谢您的建议。我刚刚用调试器检查过,结果 tile.col 是 -8448 而 YELLOW 是 -5376。但是,它们也与 when 块中的其他行不匹配。我会用 Yellow_edge 更新问题。

标签: android xml kotlin


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢您花时间找出答案!我尝试了您的解决方案,并在这样做的过程中找到了错误的真正来源。事实证明,导入不是问题(我根本没有导入颜色,而是使用了全局变量),但我不小心重新定义了 PlayActivity 类中的颜色,不知何故在when 中无法识别黄色Player 类中的语句。至少它现在有效。再次感谢您的宝贵时间:)
  • 很高兴它仍然有帮助!我真的以为我找到了解决方案嘿嘿;P
猜你喜欢
  • 2013-03-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2015-12-11
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多