2014-06-17 63 views
0

我想要做的是建立一個圖像按鈕的操作欄,我想要做的是當我按下按鈕下拉菜單中有三個項目,每次我按它帶我到正確的活動項目。這裏是我的代碼:自定義操作欄上的下拉菜單

這裏是操作欄中的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="@drawable/black_pattern" > 

    <TextView 
     android:id="@+id/title_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:textAllCaps="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#fff" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="8dp" 
     android:src="@drawable/ic_launcher" /> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="8dp" 
     android:background="@null" 
     android:src="@android:drawable/ic_menu_rotate" /> 

</RelativeLayout> 

這裏是Java文件:

公共類MainActivity擴展活動{

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ActionBar mActionBar = getActionBar(); 
     mActionBar.setDisplayShowHomeEnabled(false); 
     mActionBar.setDisplayShowTitleEnabled(false); 
     LayoutInflater mInflater = LayoutInflater.from(this); 

     View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
     TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
     mTitleTextView.setText("My Own Title"); 

     ImageButton imageButton = (ImageButton) mCustomView 
       .findViewById(R.id.imageButton); 
     imageButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplicationContext(), "Refresh Clicked!", 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 

     mActionBar.setCustomView(mCustomView); 
     mActionBar.setDisplayShowCustomEnabled(true); 
    } 

} 

所以我需要是當我點擊圖像按鈕,下拉使用下面的值列表顯示:

String[] actions = new String[] { 
     "Bookmark", 
     "Subscribe", 
     "Share" 
    }; 

,每個人帶我到正確的活動,我在安卓新..請幫助..

回答

0
package info.androidhive.actionbar; 

    import info.androidhive.actionbar.model.SpinnerNavItem; 
    import info.androidhive.info.actionbar.adapter.TitleNavigationAdapter; 

    import java.util.ArrayList; 

    import android.app.ActionBar; 
    import android.app.Activity; 
    import android.app.SearchManager; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.widget.SearchView; 

    public class MainActivity extends Activity implements ActionBar.OnNavigationListener{ 

     // action bar 
     private ActionBar actionBar; 

     // Title navigation Spinner data 
     private ArrayList<SpinnerNavItem> navSpinner; 

     // Navigation adapter 
     private TitleNavigationAdapter adapter; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      actionBar = getActionBar(); 

      // Hide the action bar title 
      actionBar.setDisplayShowTitleEnabled(false); 

      // Enabling Spinner dropdown navigation 
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 

      // Spinner title navigation data 
      navSpinner = new ArrayList<SpinnerNavItem>(); 
      navSpinner.add(new SpinnerNavItem("Local", R.drawable.ic_location)); 
      navSpinner.add(new SpinnerNavItem("My Places", R.drawable.ic_my_places)); 
      navSpinner.add(new SpinnerNavItem("Checkins", R.drawable.ic_checkin)); 
      navSpinner.add(new SpinnerNavItem("Latitude", R.drawable.ic_latitude));  

      // title drop down adapter 
      adapter = new TitleNavigationAdapter(getApplicationContext(), navSpinner); 

      // assigning the spinner navigation  
      actionBar.setListNavigationCallbacks(adapter, this); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
     .... 
     } 

     /** 
     * On selecting action bar icons 
     * */ 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
     ... 
     } 

     /** 
     * Actionbar navigation item select listener 
     * */ 
     @Override 
     public boolean onNavigationItemSelected(int itemPosition, long itemId) { 
     // Action to be taken after selecting a spinner item 
      return false; 
     } 
    } 


Try This code : 


For better understanding go through following link 
http://www.androidhive.info/2013/11/android-working-with-action-bar/ 
+0

非常感謝你 – user3675605