2012-07-23 49 views
0

我正在制作一个带有2个菜单项的sherlock动作栏。但是在动作栏的视图中,这两个项目都存在于极端的左侧位置。我想将另外1个菜单项添加到最左侧在行动bar.This中央的文本一起操作栏是代码:sherlock动作栏视图

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


} 

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

+0

一些帮助fr om老年人 – 2012-07-23 14:59:52

+0

@Chinaski先生您的答案不会在这里显示 – 2012-07-23 15:00:36

+0

我没有添加任何答案,只是用actionbarsherlock标签重新打上了问题。 – 2012-07-23 15:08:23

回答

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