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

时间:2018-09-11 20:11:57

标签: java android android-fragments kotlin

我有一个以片段为主要内容的活动。 此活动具有一个内部带有TextView的工具栏:

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>

这是我正在使用的toolbar.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>

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

我尝试从Fragment访问TextView,但出现错误

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.NullPointerException:尝试调用虚拟   方法'android.view.View   空对象上的android.support.v7.widget.Toolbar.findViewById(int)'   参考

错误来自此行:     valtoolbarTextView = toolbar.findViewById(R.id.toolBarText1)

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

2 个答案:

答案 0 :(得分:3)

您必须等到创建Activity为止:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    activity.actionBar.title = ""
}

由于您在setSupportActionBar(...)中进行过Activity,因此getActionBar()应该null

编辑: 如果您想使用自定义标题TextView,那么您的findViewById()方法也应该可行。重要的是只是等待创建Activity为止。

答案 1 :(得分:0)

这是您的操作方式:

创建一个类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类,并根据需要进行任何更改