如何给Android的ActionBar添加点击事件

如题所述

用CustomView。

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#FFF"
android:text="我才是标题" />

</LinearLayout>
部分代码:

View customView = LayoutInflater.from(this).inflate(R.layout.back, new LinearLayout(this), false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(customView);
customView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "....", 10000).show();
}
});
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答