ActionItem的使用示例

activity_main.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=".ActionItemDemoActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义动作项"
        android:textSize="25dp" />

</RelativeLayout>

res/menu/main.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/item"
        android:icon="@drawable/gift"
        android:showAsAction="ifRoom|withText"
        android:title="礼物">
    </item>
    <item android:id="@+id/item1"
        android:icon="@drawable/gift"
        android:showAsAction="never"
        android:title="礼物1">
    </item>
    

</menu>

ActionItemDemoActivity.java:

package com.example.demoactionitem;

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

public class ActionItemDemoActivity extends Activity {

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		Toast.makeText(this, "选择:"+item.getTitle(), Toast.LENGTH_LONG).show();
		return true;
	}

}


你可能感兴趣的:(ActionItem的使用示例)