当活动使用onUserInteraction()时,如何在工具栏的项目上获取onClick事件

时间:2016-02-20 10:20:05

标签: android android-toolbar

当活动使用onUserInteracion()方法时,是否可以在工具栏的项目上获取onClick()事件?

1 个答案:

答案 0 :(得分:0)

工具栏基本上只是一个ViewGroup,所以我们可以添加一个TextView并听取类似的onClick事件。

例如,你有一个XML文本到工具栏,如下所示:

<android.support.v7.widget.Toolbar
android:id="@+id/tb"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Settings"
    android:id="@+id/tb_title" />

</android.support.v7.widget.Toolbar>

在点击中听取活动:

tb.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    }
});
enter code here