2014-12-08 28 views
5

Toolbar: nút menu tràn luôn hiển thị

Vấn đề: sau khi cập nhật thư viện hỗ trợ và sử dụng thanh công cụ, nút menu tràn luôn hiển thị trên các thiết bị có và không có nút menu phần cứng

Những gì tôi Cần: Tôi muốn nút menu mục bổ sung chỉ hiển thị khi thiết bị không có nút menu phần cứng

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" > 

<item 
    android:id="@+id/action_settings" 
    app:showAsAction="never" 
    android:title="@string/action_settings"/> 

<item 
    android:id="@+id/import_data" 
    app:showAsAction="never" 
    android:title="@string/import_data"/> 

trong hoạt độ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.main, menu); 
    return true; 
} 

và trong onCreate: setSupportActionBar (mToolbar);

Trợ giúp sẽ được đánh giá cao!

+0

Mã của bạn là gì? –

+0

@Azael xem chỉnh sửa –

+0

thử ứng dụng: showAsAction = "always" – mmlooloo

Trả lời

2

tôi tìm thấy giải pháp cho vấn đề của tôi:

1- đừng gọi setSupportActionBar(mToolbar); nữa, thay vì sử dụng thanh công cụ trực tiếp

2- kiểm tra xem thiết bị có một nút menu phần cứng bằng cách gọi ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(getApplicationContext()));:

3 nếu điện thoại có nút menu tôi trở thành sự thật vào trong onCreateOptionsMenu, khác tôi thổi phồng menu trong Toolbar

+1

Tuy nhiên, có vẻ như chúng tôi đã mất một số chức năng ở đó .. – milosmns

+0

Không hoạt động ở đây – Leebeedev

2

Y Bạn có thể thay đổi hành vi của nút menu phần cứng khi có mặt để hiển thị/ẩn trình đơn mục bổ sung của thanh công cụ. Để làm như vậy, hãy ghi đè phương thức onKeyUp của Activity.

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_MENU) { 
     if (mToolbar.isOverflowMenuShowing()) { 
      mToolbar.hideOverflowMenu(); 
     } else { 
      mToolbar.showOverflowMenu(); 
     } 
     return true; 
    } 
    return super.onKeyUp(keyCode, event); 
} 
0

Nó hoạt động tốt (ít nhất là đối với tôi).

Trong phương thức onCreate hoạt động của bạn làm:

boolean hasHarwareMenu = ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(getApplicationContext())); 
    if (!hasHarwareMenu) setSupportActionBar(toolbar); 

Và thổi phồng menu.xml của bạn như bình thường trong onCreateOptionsMenu.

Các vấn đề liên quan