系統的和自定義的,如圖:小機器人是自定義的
系統的:
1.menu檔案中定義一個item,設定其屬性:android:actionProviderClass="android.widget.ShareActionProvider"
2.主布局檔案裡放一個TextView用做分享的資料來源
3.主邏輯代碼中:
(1)得到一個ActionProvider對象:ShareActionProvider provider=(ShareActionProvider) item.getActionProvider();
(2)建立一個監聽對象,設定其标記為發送:Intent sin=new Intent(Intent.ACTION_SEND);
(3)設定監聽對象分享的資料類型:sin.setType("text/plain");
(4)設定監聽對象分享的内容:sin.putExtra(Intent.EXTRA_TEXT, "分享的内容是:"+tv.getText().toString());
(5)--可無:設定監聽對象分享的曆史記錄儲存的位置: provider.setShareHistoryFileName("sh.xml");
(6)設定ActionProvider對象分享監聽(把上面建立的監聽設定進來):provider.setShareIntent(sin);
自定義的:
1.建立一個繼承ShareActionProvider的類,實作其傳上下文參數的構造方法,onCreateActionView()方法
2.建立一個view需要用的布局檔案
3.在onCreateActionView()方法中寫view對象
4.在menu檔案中menu檔案中再定義一個item,設定其屬性:android:actionProviderClass="com.example.day19_actionprovider.MyShareAP" 包名加類名
主邏輯代碼檔案:
<span style="font-size:18px;">package com.example.day19_actionprovider;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
}
@SuppressLint("NewApi") @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item=menu.findItem(R.id.share);
ShareActionProvider provider=(ShareActionProvider) item.getActionProvider();
Intent sin=new Intent(Intent.ACTION_SEND);
sin.setType("text/plain");//分享的資料類型
sin.putExtra(Intent.EXTRA_TEXT, "分享的内容是:"+tv.getText().toString());
//設定分享的曆史記錄儲存的位置
provider.setShareHistoryFileName("sh.xml");
provider.setShareIntent(sin);
return true;
}
}
</span>
主布局檔案:
<span style="font-size:18px;"><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=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生活如飲水,冷暖自知" />
</RelativeLayout>
</span>
自定義的邏輯代碼檔案:
<span style="font-size:18px;">package com.example.day19_actionprovider;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ShareActionProvider;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MyShareAP extends ShareActionProvider{
Context context;
public MyShareAP(Context context) {
super(context);
this.context=context;
}
@Override
public View onCreateActionView() {
View view=LayoutInflater.from(context).inflate(R.layout.style, null);
ImageView iv=(ImageView) view.findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "點選自定義的ActionProvider", 0).show();
}
});
return view;
}
}
</span>
自定義用到的布局檔案:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</span>
menu菜單檔案:
<span style="font-size:18px;"><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/share"
android:orderInCategory="100"
android:showAsAction="always"
android:title="分享"
android:actionProviderClass="android.widget.ShareActionProvider"/>
<item
android:id="@+id/share2"
android:orderInCategory="100"
android:showAsAction="always"
android:actionProviderClass="com.example.day19_actionprovider.MyShareAP"/>
</menu>
</span>