How to add action bar

2019-08-11 03:56发布

问题:

I want to add action bar on top of my activity. How to add action bar in the following layout. And I want to create action bar for API level less than 11. Can anyone provide me tutorial reference of that.

Layout-

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/buddha"
    android:gravity="center"
    android:orientation="vertical"
    >
    <View
        android:id="@+id/top1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="5"
    />

    <Button
        android:id="@+id/hist"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#73000000"
        android:gravity="center"
        android:textSize="@dimen/btxt"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:layout_weight="1"
        android:text="@string/his"
    />

    <View
        android:id="@+id/top2"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight=".10"
    />

    <Button
        android:id="@+id/typ"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#73000000"
        android:textSize="@dimen/btxt"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:layout_weight="1"
        android:text="@string/typ"
    />

    <View
        android:id="@+id/top3"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight=".10"
    />

    <Button
        android:id="@+id/ben"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="@dimen/btxt"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="#73000000"
        android:text="@string/ben"
    />

    <View
        android:id="@+id/top4"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight=".10"
    />

    <Button
        android:id="@+id/exit"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:textSize="@dimen/btxt"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="#73000000"
        android:layout_weight="1"
        android:text="@string/exit"
    />

    <View
        android:id="@+id/top41"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight=".10"
    />

</LinearLayout>

回答1:

setup your project to appcompat v7 library for reference see http://developer.android.com/guide/topics/ui/actionbar.html#Adding
and

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
        | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
  }


回答2:

The compatibility library has support for actionbar that works on old devices. This blog post explains how to use it and provides code samples: http://android-developers.blogspot.co.uk/2013/08/actionbarcompat-and-io-2013-app-source.html



回答3:

You need to create a menu.xml files in res/menu folder.

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_refresh"
        android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings">
    </item>

</menu> 

Then, you need to inflate and add it in your actionbar using onCreateOptionsMenu() method in your MainActivity class.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return true;
} 

And since you want to use action bar on API level less than 11, there are two popular options

  • to use actionbarsherlock
  • or ActionBarCompat library from android support library (which you can find in android developer page).

ActionBarSherlock tutorial: Adding ActionBarSherlock to Your Project