动作提供者(ActionProvider)在动作栏中的使用

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    style="?android:attr/actionButtonStyle"
    android:layout_gravity="center"
    android:addStatesFromChildren="true"
    android:background="?android:attr/actionBarItemBackground"
    android:focusable="true">
    
    <ImageButton 
        android:id="@+id/button"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="设置"
        android:scaleType="fitCenter"/>
    

</LinearLayout>

在res/menu/main.xml:

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

    <item android:id="@+id/menu_item_action_provider_action_bar"
        android:actionProviderClass="com.example.demoactionprovider.SettingsActivity$SettingsActionProvider"
        android:showAsAction="ifRoom"
        android:title="设置1"/>
    
    <item android:id="@+id/menu_item_action_provider_overflow"
        android:actionProviderClass="com.example.demoactionprovider.SettingsActivity$SettingsActionProvider"
        android:showAsAction="never"
        android:title="设置2"/>

</menu>

SettingsActivity.java:

package com.example.demoactionprovider;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public class SettingsActivity 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 static class SettingsActionProvider extends ActionProvider{
		private static final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
		private Context context;
		public SettingsActionProvider(Context context){
			super(context);
			this.context = context;
		}
		
		public View onCreateActionView(){
			LayoutInflater inflater = LayoutInflater.from(context);
			View view = inflater.inflate(R.layout.action_provier, null);
			ImageButton button = (ImageButton)view.findViewById(R.id.button);
			
			button.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {

					context.startActivity(settingsIntent);
				}
			});
			return view;
		}
		
		public boolean onPerformDefaultAction(){
			context.startActivity(settingsIntent);
			return true;
		}
	}

}


你可能感兴趣的:(动作提供者(ActionProvider)在动作栏中的使用)