android开发如何创建动作栏

时间:2013-10-01 07:20:20

标签: android api android-actionbar

我正在使用API​​ 15使用操作栏创建android开发,但问题是我希望操作栏位于屏幕顶部。在我的情况下,行动在屏幕上,任何人都可以帮助我???

ActionBarTest.java

package com.lebdev.actionbardemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class ActionBarTest extends Activity {

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.iSearch:
            search();
            return true;

        case R.id.iPhoto:
            photo();
            return true;

        case R.id.iVideo:
            video();
            return true;
        default:

        }
        return super.onOptionsItemSelected(item);
    }

    private void video() {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "This is video function", Toast.LENGTH_SHORT).show();
    }

    private void photo() {
        // TODO Auto-generated method stub

        Toast.makeText(getBaseContext(), "This is photo function", Toast.LENGTH_SHORT).show();

    }

    private void search() {
        // TODO Auto-generated method stub

        Toast.makeText(getBaseContext(), "This is search function", Toast.LENGTH_SHORT).show();
    }
}

activity_action_bar_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ActionBarTest" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBarDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="tsearch">Search</string>
    <string name="tphoto">Photo</string>
    <string name="tvideo">Video</string>

</resources>

menu.xml文件

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

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/iSearch"
        android:orderInCategory="1"
        android:showAsAction="ifRoom|withText"
        android:title="@string/tsearch">
    </item>
    <item
        android:id="@+id/iPhoto"
        android:orderInCategory="2"
        android:showAsAction="ifRoom|withText"
        android:title="@string/tphoto">
    </item>
    <item
        android:id="@+id/iVideo"
        android:orderInCategory="3"
        android:showAsAction="ifRoom|withText"
        android:title="@string/tvideo">
    </item>

</menu>

2 个答案:

答案 0 :(得分:0)

像这样使用你的代码..

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/iSearch"
    android:orderInCategory="1"
    android:showAsAction="always|withText"
    android:title="@string/tsearch">
</item>
<item
    android:id="@+id/iPhoto"
    android:orderInCategory="2"
    android:showAsAction="always|withText"
    android:title="@string/tphoto">
</item>
<item
    android:id="@+id/iVideo"
    android:orderInCategory="3"
    android:showAsAction="always|withText"
    android:title="@string/tvideo">
</item>

让我知道..

答案 1 :(得分:0)

如果你的Android应用程序的最小sdk是“API 15”

 android:minSdkVersion="15"

然后你可以使用在Api等级11中引入的ActionBar

或者您可以使用ActionBar (Github)