如何给Android的ActionBar添加点击事件
1个回答
展开全部
用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();
}
});
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();
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询