以编程方式从 Kotlin 中的片段更改工具栏文本



我有一个以片段为主要内容的活动。 此活动有一个工具栏,其中包含文本视图:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(my_activity)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
supportActionBar!!.setDisplayShowTitleEnabled(false)
}

以下是该活动的 xml 文档中的工具栏:

<?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"
tools:context=".MyActivity">
<include
android:id="@+id/toolbar"
layout="@layout/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<fragment android:name="org.siku.siku.MyFragment"
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/my_fragment" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>

这是我正在使用的工具栏.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/closeBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/titleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/toolBarText1"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>

我可以轻松地从该活动更改文本,但我无法更改片段中的文本:

我尝试从片段访问文本视图,但出现错误

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
myView = inflater.inflate(R.layout.my_frament, container, false)
val toolbar = activity!!.findViewById<Toolbar>(R.id.toolbar)
val toolbarTextView = toolbar.findViewById<TextView>(R.id.toolBarText1)
}

这是我得到的错误:

由以下原因引起:java.lang.NullPointer异常:尝试调用虚拟 方法 'android.view.view android.support.v7.widget.Toolbar.findViewById(int(' on a null object 参考

错误来自以下行: val toolbarTextView = toolbar.findViewById(R.id.toolBarText1(

这意味着我没有正确检索该 TextView,但从我看到的示例来看,这应该是正确的方法。

您必须等到创建Activity

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
activity.actionBar.title = ""
}

因为你在Activity里做了setSupportActionBar(...),所以getActionBar()不应该被null

编辑: 如果要使用自定义标题 TextView,则findViewById()方法也应该有效。重要的是等到创建Activity

以下是您可以操作的方法:

创建一个类ToolbarController

class ToolbarController(val toolbar: View) {
fun setTitle(title: String) {
toolbar.toolBarText1.visibility = View.VISIBLE
toolbar.toolBarText1.text = title
}
}

在您的活动中:

public var toolBarController: ToolbarController? = null

并覆盖onPostCreate

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
toolBarController = ToolbarController(topBar)
}

现在在片段onCreateView创建一个调用一个名为initToolbar的方法,定义为:

private fun initToolbar() {
(activity as YourActivity)?.toolBarController?.setTitle("My title")
}

您可以修改ToolbarController类并根据需要进行任何更改

最新更新