編輯/簡短的回答:如果使用AppCompat的ShareActionProvider,只需在主題定義中提供一個新的actionModeShareDrawable
。
<style name="MyTheme" parent="Theme.AppCompat">
<item name="actionModeShareDrawable">@drawable/my_share_drawable</item>
</style>
如果使用程序兼容性,然後this resource is defined for Lollipor or newer,但不是以前的版本不。
下面是本機ShareActionProvider
(這是這個問題的原始範圍)的答案。
要更改此圖片,您應該更改您應用主題的值actionModeShareDrawable
。在ShareActionProvider
的onCreateActionView()
方法請看:
public View onCreateActionView() {
// Create the view and set its data model.
...
// Lookup and set the expand action icon.
TypedValue outTypedValue = new TypedValue();
mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true);
Drawable drawable = mContext.getResources().getDrawable(outTypedValue.resourceId);
...
不幸的是這個屬性不公共Android框架(儘管它是如果使用兼容庫,如程序兼容性或ActionBarSherlock)。在這種情況下,這只是一個壓倒主題價值的問題。
如果您使用的都不是這些庫,唯一的解決方案(我知道)是創建一個ShareActionProvider
的子類並重新實現onCreateActionView()
方法。然後你可以使用任何你想要的drawable。
編輯然而,onCreateActionView()
的實現使用其他類不公開的事實,這一點變得更加複雜。爲了避免大量重複的代碼,你可以通過反射更改圖標,就像這樣:
public class MyShareActionProvider extends ShareActionProvider
{
private final Context mContext;
public MyShareActionProvider(Context context)
{
super(context);
mContext = context;
}
@Override
public View onCreateActionView()
{
View view = super.onCreateActionView();
if (view != null)
{
try
{
Drawable icon = ... // the drawable you want (you can use mContext to get it from resources)
Method method = view.getClass().getMethod("setExpandActivityOverflowButtonDrawable", Drawable.class);
method.invoke(view, icon);
}
catch (Exception e)
{
Log.e("MyShareActionProvider", "onCreateActionView", e);
}
}
return view;
}
}
與涉及反射的任何解決方案,這可能是易碎的,如果內部實現在未來ShareActionProvider
變化。
mhhh,我不明白如何重新實現oncreateactionview,這個函數中的初始代碼是什麼? – deveLost
@devLost原始代碼位於https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ShareActionProvider.java您是否確實沒有使用'appcompat'庫?因爲如果你這樣做了,它會更容易,這種方法需要複製大量的框架代碼。 – matiash
mhh,我試圖修改這段代碼,改變drawable,但是我有很多錯誤。我會再試一次。如果我導入appcompat庫,會發生什麼變化? – deveLost