简体   繁体   English

为什么在Android 4.0+上支持v7 actionbar进入屏幕底部

[英]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? 我在android中有一个简单的应用程序(开发中),我想使其与2.0-> 4.3兼容,因此我想使用actionbar(阅读支持v7),当我编写代码并在android 2.3中执行时,例如,actionbar (在这里看到我在说什么的图像),但是在vm 4.0+中运行时不知道为什么,但是动作项进入了android的底部(请参阅我在说什么的图像)如何将代码更改为平台上的行为是否相同?

MainActivity.java 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.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 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 那是因为AndroidManifest.xml上的android:uiOptions="splitActionBarWhenNarrow"属性告诉Android如果顶部没有足够的空间,则拆分操作菜单

This attribute is understood only by API level 14 and higher (it is ignored by older versions). 只有API级别14和更高版本才能理解此属性(旧版本将忽略此属性)。

To support older versions, add a element as a child of each element that declares the same value for "android.support.UI_OPTIONS". 为了支持较旧的版本,请将一个元素添加为为“ 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 有关更多信息,请参阅Android文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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