Waz*_*_Be 29 android onclick title textview android-actionbar
对于特定的客户需求,我需要允许我的应用程序的用户(不会在Market中发布)单击ActionBar标题来执行某些操作.
我一直在寻找Android源代码,但我无法找到actionBar TextView标题的ID.
是否有正确的方法来处理此类点击?
Com*_*are 28
标题是不可点击的AFAIK.图标/徽标是可点击的 - 你可以通过它android.R.id.home获得onOptionsItemSelected().可以想象,标题也采用这种方式,虽然他们没有提到它,我不会依赖它.
听起来您希望Spinner用户选择要执行的操作.如果是这样,请使用setListNavigationCallbacks().如果您想删除标题,因为现在是多余的,请使用setDisplayOptions(0, DISPLAY_SHOW_TITLE).
如果您想要Spinner操作栏左侧的其他内容,请使用setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM)和setCustomView().请注意,不建议使用此方法("避免在操作栏中使用自定义导航模式"),因为它可能无法与手机配合使用,尤其是在纵向模式下.
另一种可能性是删除标题并使用徽标而不是图标,并在徽标中将您的"标题"作为图像的一部分.整个徽标应该是可点击的,通过onOptionsItemSelected().
//的onCreate
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
// View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//your logic for click listner
setListenerForActionBarCustomView(actionBarView);
private void setListenerForActionBarCustomView(View actionBarView) {
ActionBarCustomViewOnClickListener actionBarCustomViewOnClickListener = new ActionBarCustomViewOnClickListener();
actionBarView.findViewById(R.id.text_view_title).setOnClickListener(actionBarCustomViewOnClickListener);
}
private class ActionBarCustomViewOnClickListener implements OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
case R.id.text_view_title:
//finish();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过<android.support.v7.widget.Toolbar>在布局中声明来设置支持库中的自定义工具栏(请参阅Chris Banes对完整工具栏布局示例的回答).
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_toolbar"
android:minHeight="?attr/actionBarSize" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在您的活动中添加点击监听器之后,就像大多数其他视图一样.
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show();
}
});
Run Code Online (Sandbox Code Playgroud)
如果要捕获标题上的触摸事件:
toolbar.setOnTouchListener(new View.OnTouchListener() {
Rect hitrect = new Rect();
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
boolean hit = false;
for (int i = toolbar.getChildCount() - 1; i != -1; i--) {
View view = toolbar.getChildAt(i);
if (view instanceof TextView) {
view.getHitRect(hitrect);
if (hitrect.contains((int)event.getX(), (int)event.getY())) {
hit = true;
break;
}
}
}
if (hit) {
//Hit action
}
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)