我正在使用 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>
字符串.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>
菜单.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>