谢尔洛克动作栏视图

时间:2012-07-23 14:50:50

标签: android actionbarsherlock

我正在制作带有2个菜单项的sherlock操作栏。但是在操作栏的视图中,这两个项目都位于最左侧的位置。我想在操作栏的最左侧添加1个菜单项在操作栏的中心有一个文本。这是代码:

package naseeb.bar;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;


public class NaseebactionbarActivity extends SherlockActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newlayout);
        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayShowTitleEnabled(false);
        actionbar.setDisplayShowHomeEnabled(false);
        actionbar.show();  

        }

    public void newclickhandler(View view)
    {
        Toast.makeText(NaseebactionbarActivity.this, "press the button to choose the time limit for the timer", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(NaseebactionbarActivity.this,controltransfer.class);
        startActivity(intent);


    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu ) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item){
        // same as using a normal menu
        switch(item.getItemId()) {
        case R.id.menu1:

           LayoutInflater inflater=getLayoutInflater();
          View view = inflater.inflate(R.layout.main,null);
           view.setMinimumWidth(200);
            view.setMinimumHeight(200);

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setView(view);
            alertDialog.show();


            break;
        case R.id.menu2:
            Intent intent = new Intent(NaseebactionbarActivity.this,controltransfer.class);
            startActivity(intent);
            break;
        }

        return true;
    }

    public void makeToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }   


}

请告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您需要使用自定义操作布局

添加菜单项

menu_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/mb_search_text"
        android:title="@null"
        android:actionLayout="@layout/menu_item_layout"
        android:showAsAction="always"/>

</menu>

menu_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button Left" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_centerHorizontal="true"
        android:text="Title" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button Right" />

</RelativeLayout>

并在SherlockActivity中充气:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.menu_layout, menu);
        return true;
    }