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

2025-03-20 15:43:58
推荐回答(1个)
回答(1):

用CustomView。

layout:


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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


部分代码:

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();
}
});