2013-10-14 16 views
6

tôi sử dụng thư viện actionbarsherklock với thanh hành động tùy chỉnh giống như thế này:Tuỳ chỉnh actionbar bố trí với thực đơn tràn

enter image description here

tùy chỉnh của tôi thực hiện:

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

Và đây là cách bố trí tùy chỉnh:

<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" /> 

Vấn đề là tôi muốn thêm một thực đơn trên dòng chảy để chia sẻ nút như thế này:

enter image description here

Hãy cho tôi biết làm thế nào tôi có thể làm điều đó với cách bố trí thanh hành động tùy chỉnh.

+0

không điện thoại của bạn có một nút menu phần cứng? – Raghunandan

+0

Tôi hỗ trợ nhiều thiết bị từ Android 2.3 nên tôi nghĩ có. – R4j

+0

http://android-developers.blogspot.in/2012/01/say-goodbye-to-menu-button.html. điều này có thể giúp – Raghunandan

Trả lời

3

Chỉ cần ghi đè này hai phương pháp để gọi trình đơn tràn trong hoạt động của bạn mà người ta đang mở rộng 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

bạn không nhận được điểm. Vì tôi sử dụng bố cục tùy chỉnh của riêng mình cho thanh tác vụ nên tôi không thể thêm menu vào nút chia sẻ như hình ảnh ở trên. – R4j

+2

@ R4j Tôi cũng sử dụng thanh tác vụ tùy chỉnh của mình và mở cùng một menu thả xuống như hình trên, vì vậy hãy trả lời câu hỏi của riêng bạn với giải pháp khác và làm cho giải pháp phù hợp không phải là cách thích hợp !!! –