2013-10-14 13 views
6

Io uso la biblioteca actionbarsherklock con bar un'azione personalizzata simile a questa:personalizzato ActionBar disposizione con menu di overflow

enter image description here

mia abitudine implementare:

ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

    // Do any other config to the action bar 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 
    getSupportActionBar().setDisplayShowHomeEnabled(false); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    // set custom view 
    View actionBarView = getLayoutInflater().inflate(
      R.layout.action_bar_default, null); 

    View btnMenuLeft= actionBarView.findViewById(R.id.btnMenuLeft); 
    btnMenuLeft.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      toggle(); 
     } 
    }); 

    View btnMenuShare= actionBarView.findViewById(R.id.btnMenuShare); 
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    actionBar.setCustomView(actionBarView, params); 

    // Hide the home icon 
    actionBar.setIcon(android.R.color.transparent); 
    actionBar.setLogo(android.R.color.transparent); 

Ed ecco il layout personalizzato:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/nav_bar_bg" 
android:gravity="center" 
android:orientation="horizontal" > 

<!-- menu button --> 
    <ImageButton 
     android:id="@+id/btnMenuLeft" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/list_btn" 
     android:clickable="false" 
     android:duplicateParentState="true" 
     android:focusable="false" /> 

    <!-- logo --> 
    <ImageView  
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:src="@drawable/app_logo" /> 

    <!-- share button --> 
    <ImageButton 
     android:id="@+id/btnMenuShare" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/action_btn" 
     android:clickable="false" 
     android:duplicateParentState="true" 
     android:focusable="false" /> 

Il problema è che voglio aggiungere un menu di flusso su pulsante per condividere come questo:

enter image description here

Ti prego, dimmi come posso fare che con il layout della barra un'azione personalizzata.

+0

Il vostro dispositivo ha un pulsante del menu hardware? – Raghunandan

+0

Supporto molti dispositivi di Android 2.3 quindi penso di sì. – R4j

+0

http://android-developers.blogspot.in/2012/01/say-goodbye-to-menu-button.html. questo potrebbe aiutare – Raghunandan

risposta

3

Basta sovrascrivere questo due metodi per chiamare menu di overflow nella vostra attività, che si sta estendendo ActionBarActivity:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.your_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

non si ottiene il punto. Perché utilizzo il mio layout personalizzato per la barra delle azioni, quindi non posso aggiungere menu nel pulsante Condividi come sopra. – R4j

+2

@ R4j Ho anche usato la mia barra azioni personalizzata e aprendo lo stesso menu a discesa come sopra immagine, quindi rispondi alla tua domanda con una soluzione diversa e rendila una soluzione giusta non è un modo corretto !!! –

Problemi correlati