如何ActionBar Sherlock标题被发现?

时间:2014-01-01 06:42:44

标签: android android-intent android-actionbar

我想知道如何让ActionBar Sherlock标题被检测为ant以获得对此类其他活动的意图!!

This type of menu click

我知道这对你们所有人都是可能的。但是,如果我没有在谷歌或Stack OverFlow这里得到它。所以我需要问你。

3 个答案:

答案 0 :(得分:1)

这可能对你有帮助......

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

    RelativeLayout layout = new RelativeLayout(actionBar.getThemedContext());
    layout.setBackgroundColor(Color.TRANSPARENT);
    ImageView imageView = new ImageView(actionBar.getThemedContext());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    imageView.setLayoutParams(params);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setImageResource(R.drawable.ic_launcher);
    imageView.setId(0xFEED);
    layout.addView(imageView);

    TextView textView = new TextView(actionBar.getThemedContext());
    String s = "<font size='10' color='#FFFFFF'>Mine</font><br></br>You";
    textView.setText(Html.fromHtml(s));
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { android.R.attr.state_pressed },new ColorDrawable(0xFF4887A2));
    drawable.addState(new int[] { android.R.attr.state_enabled,android.R.attr.state_focused }, new ColorDrawable(Color.TRANSPARENT));
    textView.setBackgroundDrawable(drawable);
    textView.setClickable(true);
    textView.setFocusable(true);
    textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.LEFT_OF, imageView.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    textView.setLayoutParams(params);
    layout.addView(textView);

    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL
                    | Gravity.LEFT);
    layout.setLayoutParams(layoutParams);
    actionBar.setCustomView(layout);

答案 1 :(得分:0)

您可以将标题设置为向上指示符,并像其他菜单项一样将其处理。

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

然后使用以下代码:

public boolean onOptionsItemSelected(final MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            // your title was clicked!
            return true;
    }
}

并在此处How can I detect a click on the ActionBar title?

查看此问题

答案 2 :(得分:0)

使用此代码:

public class TempActivity  extends SherlockActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    ActionBar mActionBar = getSupportActionBar();
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.activity_main, null);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
    ImageButton button= (ImageButton) mCustomView.findViewById(R.id.mybutton);
    button.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "actionbar clicked", Toast.LENGTH_LONG).show();
        }
    });
}

}