2013-08-26 23 views
18

Tôi đang sử dụng ActionBarCompat trong ứng dụng của tôi, một tôi muốn hiển thị một hoặc hai mục trong thanh hành độngmục trong ActionBarCompat sẽ được trình bày luôn tràn

tôi follwed hướng dẫn các nhà phát triển google, nhưng khi tôi thử nghiệm nó , các mục được hiển thị trong tùy chọn "Tràn" (trong Nexus 4) và nếu tôi nhấn vào nút menu nếu có (ví dụ: Galaxy S3)

Tôi đã làm gì sai?

SOLUTION FOUND

Bạn có thể tìm thấy nó trong một câu trả lời.

+2

Tốt hơn để đăng giải pháp dưới dạng Câu trả lời thực tế. –

+0

@MarkCarter Sau thời gian "ít" .. Tôi thấy yêu cầu của bạn và tôi đã chỉnh sửa bài đăng của mình để chia sẻ câu hỏi cho câu trả lời. Cảm ơn (bit muộn) cho đề nghị. – Shudy

Trả lời

0

Tôi tìm thấy giải pháp vì vậy tôi đăng tải như trả lời:

SOLUTION FOUND

Trong xml của menu, bạn cần phải đặt không gian tên mới, để làm cho ActionBarCompat làm việc. Vì vậy, có một số tùy chọn cần tên spac thay vì "android". Vì vậy, giải pháp là này:

Login đơn Cũ:

<item 
     android:id="@+id/action_register" 
     android:showAsAction="always" 
     android:title="@string/login_menu_register"/> 

New Login Menu (giải pháp) (Hãy nhìn cách là caled "showAsAction")

<item 
     android:id="@+id/action_register" 
     shudy:showAsAction="always" 
     android:title="@string/login_menu_register"/> 

Login.xml (MENU)

<item 
    android:id="@+id/action_register" 
    android:showAsAction="always" 
    android:title="@string/login_menu_register"/> 
<item 
    android:id="@+id/action_register2" 
    android:showAsAction="always" 
    android:title="miau"/> 

LoginActivity.java

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SLog.d(CLASS_NAME + " onCreate()"); 
     setContentView(R.layout.activity_login); 
     actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 
     findViews(); 
     buttons(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.login, menu); 
     return true; 
    } 

Manifest

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.AppCompat.Light.DarkActionBar" > 
     <activity 
      android:name="com.shudy.myworld.LoginActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

enter image description hereenter image description here

0

Tôi nghĩ bạn cần cung cấp biểu tượng cho những hành động này để chúng có thể xuất hiện trong thanh hành động.

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

<item 
    android:id="@+id/action_register" 
    android:showAsAction="always" 
    android:icon="@drawable/some_icon" 
    android:title="@string/login_menu_register" /> 
<item 
    android:id="@+id/action_register2" 
    android:showAsAction="always" 
    android:icon="@drawable/some_icon2" 
    android:title="miau" /> 

</menu> 
+0

Không làm việc: S nhưng tôi đã tìm ra giải pháp;) Tôi sẽ chỉnh sửa câu trả lời của mình để sẵn sàng cho mọi người – Shudy

0

Câu hỏi đặt ra đã được cập nhật để bao gồm các câu trả lời, nhưng đối với những ai quan tâm đến các tài liệu chính thức, xem http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems và đặc biệt chú ý đến các lưu ý về việc sử dụng một không gian tên tùy chỉnh cho các thuộc tính showAsAction.

0
<item 
    android:id="@+id/ok" 
    android:icon="@drawable/ic_ok" 
    android:orderInCategory="0" 
    android:showAsAction="ifRoom" 
    android:title="OK"/> 
3

tôi đã cùng một vấn đề, và tìm thấy hai giải pháp:

Trong xml menu (Login.xml), sử dụng tên ứng dụng của bạn cho thẻ showAsAction:

thay vì:

<item 
    android:id="@+id/action_register" 
    android:showAsAction="always" 
    android:icon="@drawable/some_icon" 
    android:title="@string/login_menu_register" /> 

sử dụng:

<item 
    android:id="@+id/action_register" 
    yourappname:showAsAction="always" 
    android:icon="@drawable/some_icon" 
    android:title="@string/login_menu_register" /> 

Tôi cho rằng tên ứng dụng của bạn là bóng râm.

giải pháp thứ hai đối với tôi, trên lớp hoạt động, tại onCreateOptionsMenu()

public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(
    getMenuInflater().inflate(R.menu.main, menu); 
    MenuItem registerMenuItem = menu.findItem(R.id.action_register); 
    registerMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); // change this in backcompat 
    return true; 
} 

nếu bạn đang sử dụng backCompatibility, thay đổi dòng cuối cùng:

MenuItemCompat.setShowAsAction(registerMenuItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS); 
+0

Gọi hàm setShowAsAction' là ma thuật cần thiết. Tôi tự hỏi tại sao XML không hoạt động. – Synesso

+0

trong một số xml của tôi khi sử dụng ứng dụng compat, tiền tố android là cần thiết và đôi khi không, tùy thuộc vào phiên bản android đang chạy. Theo tôi nhớ, android: background không hoạt động trên android <12 và bạn chỉ cần sử dụng nền. Dường như với tôi điều tương tự đang xảy ra ở đây. –

0

Chúng tôi có hai cách để chất tan vấn đề này.

  1. Bạn cần thêm libs "ActionBarSherlock" .Đây là cách sử dụng ActionbarSherlock.

HOẶC 2. Bạn cần thêm libs "android-Support-v7". Nếu bạn chọn phương pháp này, thì menu.xml của bạn cần như sau:

<?xml version="1.0" encoding="UTF-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:v7="http://schemas.android.com/apk/res-auto" > 
<item 
    android:id="..." 
    android:icon="..." 
    android:showAsAction="always" 
    android:title="..." 
    v7:showAsAction="always"/> 
</menu> 
0

Điều này cũng đúng với Thanh công cụ Android trong các khái niệm thiết kế Material Design mới trong các mức AppCompat-v21 và API 21 trở lên.

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