简体   繁体   中英

Why support v7 actionbar goes to screen bottom on android 4.0+

I have a simple app in android ( developing ) and I want make it compatible with 2.0 -> 4.3, so I want use actionbar ( read support v7 ), when I wrote the code and perform run in android 2.3 for example the actionbar stay beauty (see here the image of what I talking about) but when run in vm 4.0+ don't know why but the action items goes to bottom of android ( see the image of what I talking about) how can I change my code to the behavior is the same in the platforms?

MainActivity.java

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {

private final String TAG = this.getClass().getSimpleName();

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

    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#CCCCCC")));

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getOrder()) {
    case 1:
        Log.i(TAG, "Tentando criar o actionbar menu.");
        View menuItemView = findViewById(R.id.action_search);
        PopupMenu popupMenu = new PopupMenu(this, menuItemView);
        popupMenu.inflate(R.menu.popup_menu);

        popupMenu.show();
        break;
    }
    return true;
}

}

menu.xml

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

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_accept"
    android:orderInCategory="1"
    android:title="Search"
    creditguard:showAsAction="always"/>
<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_accept"
    android:orderInCategory="2"
    android:title="Search"
    creditguard:showAsAction="ifRoom|never"/>

  </menu>

AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.creditguard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />

    <!-- Permissions -->
        ...

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

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

        <!-- Service declararion -->
        <service/>
            ...
        </service>

        <!-- Receiver to start service on boot -->
        <receiver/>

        </receiver>
            ...
        <!-- Widget -->
        <receiver/>
            ...
        </receiver>
    </application>

    </manifest>

Sorry if the english is bad.

That is because android:uiOptions="splitActionBarWhenNarrow" attribute on AndroidManifest.xml tells android to split the action menu if have not enough space on top

This attribute is understood only by API level 14 and higher (it is ignored by older versions).

To support older versions, add a element as a child of each element that declares the same value for "android.support.UI_OPTIONS".

<manifest ...>
    <activity uiOptions="splitActionBarWhenNarrow" ... >
        <meta-data android:name="android.support.UI_OPTIONS"
                   android:value="splitActionBarWhenNarrow" />
    </activity>
</manifest>  

For more info see Android documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM