【发布时间】:2021-11-05 21:44:39
【问题描述】:
我尝试制作一个应用程序在Android中练习布局,但我收到错误:
未解决的参考:box_three_text
看截图: [1]:https://i.stack.imgur.com/W3uGt.jpg
为什么讲师代码有效而我的无效?
这是我的代码:
MainActivity.kt
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setListeners()
}
private fun setListeners(){
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text, box_four_text, box_five_text)
for (item in clickableViews){
item.setOnClickListener{makeColored(it)}
}
}
fun makeColored(view: View) {
when (view.id) {
// Boxes using Color class colors for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes using Android color resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/box_one_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_wide"
android:layout_marginTop="@dimen/margin_wide"
android:layout_marginEnd="@dimen/margin_wide"
android:fontFamily="@font/roboto"
android:text="@string/box_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/box_two_text"
style="@style/WhiteBox"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/box_two"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/box_one_text" />
<TextView
android:id="@+id/box_three_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_three"
app:layout_constraintBottom_toTopOf="@+id/box_four_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/box_two_text"
app:layout_constraintTop_toTopOf="@+id/box_two_text" />
<TextView
android:id="@+id/box_four_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_four"
app:layout_constraintBottom_toTopOf="@+id/box_five_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/box_two_text"
app:layout_constraintTop_toBottomOf="@+id/box_three_text" />
<TextView
android:id="@+id/box_five_text"
style="@style/WhiteBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/box_five"
app:layout_constraintBottom_toBottomOf="@+id/box_two_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/box_two_text"
app:layout_constraintTop_toBottomOf="@+id/box_four_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的系统:Ubuntu 20.04 Android Studio 2021.1.1 Canary 11
【问题讨论】:
-
他们可能正在使用合成导入,此时已过时且不推荐使用。只需使用 findViewByID
标签: android android-layout android-videoview