Android 在kotlin中设置工具栏文本颜色
我一直在尝试更改工具栏中的文本颜色,但我没有取得任何进展,我尝试了许多来自堆栈溢出的答案,但要么它不起作用,要么应用程序在这一行崩溃Android 在kotlin中设置工具栏文本颜色,android,kotlin,android-toolbar,Android,Kotlin,Android Toolbar,我一直在尝试更改工具栏中的文本颜色,但我没有取得任何进展,我尝试了许多来自堆栈溢出的答案,但要么它不起作用,要么应用程序在这一行崩溃setSupportActionBar(工具栏) 以下是Kotlin的活动: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_chat) suppor
setSupportActionBar(工具栏)
以下是Kotlin的活动:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = intent.getStringExtra(AppConstants.GROUP_NAME)
}
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
tools:context=".ChatActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_messages"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/relativeLayout_message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
提前谢谢。您说您的应用程序在这一行崩溃:
setSupportActionBar(工具栏)
但是在
onCreate()
方法中没有这一行如果活动中确实有工具栏,请使用此选项更改其文本颜色:
toolbar.setTitleTextColor(Color.WHITE)
或者您喜欢的任何颜色。如果您想在所有应用程序上更改它,您可以使用样式,如中所述:
@样式/MyActionBar
@样式/MyActionBarTabText
@颜色/操作栏\文本
@样式/MyActionBartitletText
@颜色/操作栏\文本
@颜色/操作栏\文本
另一方面,如果要根据所处的活动/片段/视图自定义操作栏,则应:
- 将应用程序样式更改为不带操作栏的变体,例如
“Theme.AppCompat.Light.NoActionBar
- 将工具栏添加到活动布局中
- 使用
setSupportActionBar(工具栏)
完成此操作后,您可以引用工具栏来更改文本颜色,并从XML或编程方式完全自定义ActionBar@Mohsen它将字符串设置为正确的颜色,它显示为黑色文本,我想要白色文本您在样式父级中使用的是黑色主题,对吗?这就是为什么它是黑色的。我取出了它,但我如果它在里面,它会在那条线上崩溃
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.Holo">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">@color/actionbar_text</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/actionbar_text</item>
</style>
<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
parent="@style/Widget.Holo.ActionBar.TabText">
<item name="android:textColor">@color/actionbar_text</item>
</style>
</resources>