2013-10-01 50 views
0

我正在使用API​​ 15創建具有動作欄的android開發,但問題是我想讓動作欄位於屏幕的頂部。在我的情況下,行動是在任何人都可以幫助我的屏幕下?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> 
+0

http://developer.android.com/training/basics/actionbar/adding-buttons.html這裏閱讀 – 2013-10-01 07:22:30

+0

這是一個糟糕的設計,Android不建議底部的Action bar。 –

+0

@Brontok這就是爲什麼他希望頂部的操作欄。 – Lennart

回答

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> 

讓我知道..

+0

@ user2586685它仍然顯示在屏幕按鈕的動作欄 – LebDev

+0

等待讓我再次檢查 – Ankit

+0

嘿,我用一些改變相同的代碼....請參閱編輯的答案。 其工作正常...... – Ankit

0

如果您的Android應用的最小SDK是 「API 15」

android:minSdkVersion="15" 

然後你可以使用在Api級別11中引入的ActionBar

或者您可以使用ActionBar (Github)

相關問題