android - kotlin android微调器文本颜色
问题描述
我创建了一个微调器,但我想更改文本颜色和字体大小。网上有很多Java教程。我很难将 Java 翻译成 Kotlin,因为我是新手。请帮助找到解决方案。
SpinnerActivity.kt
class SpinnerActivity : AppCompatActivity() {
lateinit var option : Spinner
lateinit var result :TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_spinner)
option = findViewById(R.id.sp_option) as Spinner
result = findViewById(R.id.tv_result) as TextView
val options = arrayOf("111", "222", "333")
option.adapter = ArrayAdapter<String>(this,R.layout.spinner_layout,options)
option.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
result.text = "please select an option"
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
result.text = options.get(position)
}
}
activity_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SpinnerActivity">
<Spinner
android:id="@+id/sp_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:popupTheme="@android:style/ThemeOverlay.Material.Light"
/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
</LinearLayout>
spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/custom_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
/>
</RelativeLayout>
解决方案
为您所需的 textview 创建一个 xml,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@android:color/holo_red_dark"
android:textSize="24sp" />
将其命名为 item.xml 并在您的活动类中更改以下内容。
option.adapter = ArrayAdapter<String>(this, R.layout.item, options)
而已!!!
推荐阅读
- android - 无法从嵌套的实时数据库中获取要在 recyclerview 中显示的图像
- mysql - 使用转义的 req.body 值数组使用 mysql 插入 SQL 语法错误
- perl - 转义多字节字符
- angular - Angular 中的组件是一种指令?
- database - 涉及大量磁盘 I/O 的大批量处理的并行方法
- html - 带有滚动和三角形的CSS Div,右侧箭头
- swift - 无法通过 id 找到约束
- docker - 如何在 Windows 2 下使用 Docker 在不同的 URL 下启动和访问不同的 Webserver?
- python - Python - 在异常中捕获异常?
- javascript - 使用静态获取未定义