gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:56:16 30 4
gpt4 key购买 nike

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

我正在制作一个游戏,用户最终进入 PlayActivity 并从“袋子”中抽取一些图 block 。这些图 block 有一个大写颜色的数字(例如 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>

我用 TextView 填充 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.xmlblue_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>

最佳答案

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

您将颜色定义为常量,然后必须将其导入到 ActivityPlayer 类中。

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

关于android - 调用后 TextView 边框颜色未设置为黄色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607071/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com