Setting a custom share icon on Actionbar ShareActionProvider

前端 未结 5 539
情书的邮戳
情书的邮戳 2021-01-01 20:28

I\'m trying to set the icon ShareActionProvider, need a solid white one instead of the semi transparent white one.

However setting in share_menu.xml and code doesn

相关标签:
5条回答
  • 2021-01-01 20:42

    UPDATE: Forget about the TRICKY way below, just sub-class ShareActionProvider, return null in onCreateActionView Method, and provide your own icon in menu.xml file, everything will be fine

    ===========================

    I found a very tricky but none-ActionBarSherlock-specific way:

    1. Sub-class ShareActionProvider, with just a little tweak:

      @Override
      public View onCreateActionView() {
          View target = super.onCreateActionView();
          ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.actionbutton_share, null);
          ImageView overlay = (ImageView) viewGroup.findViewById(R.id.overlay);
          ViewGroup container = (ViewGroup) viewGroup.findViewById(R.id.container);
          container.addView(target);
          container.getLayoutParams().width = overlay.getLayoutParams().width;
          container.getLayoutParams().height = overlay.getLayoutParams().height;
          return viewGroup;
      }
      
    2. Create a layout file (R.layout.actionbutton_share), which place the original view at top but transparent to user (Note the "android:alpha=0" part) :

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <FrameLayout
              android:id="@+id/container"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:alpha="0" />
      
          <ImageView
              android:id="@+id/overlay"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:layout_marginLeft="7dp"
              android:layout_marginRight="7dp"
              android:src="@drawable/yours_whatever_share_icon"
              tools:ignore="ContentDescription" />
      
      </RelativeLayout>
      
    3. Use the hacked ShareActionProvider in your menu.xml file

    0 讨论(0)
  • 2021-01-01 20:43

    When I am looking to customize the action bar, usually the first place I look is the ActionBarSherlock themes and styles.

    I found that the Sherlock theme uses the "actionModeShareDrawable" as found in theme.xml file.

    Try changing your theme to include the "actionModeShareDrawable" item directly.

    <style name="Theme.MyApp" parent="Theme.Sherlock.Light">
        <item name="actionModeShareDrawable">@drawable/ic_action_share</item>
    </style>
    
    0 讨论(0)
  • 2021-01-01 20:52

    vivimice's solution is good, but the icon of last used app for sharing becomes hidden too.

    I did custom share icon as a common menu item without any ShareActionProvider, just in onOptionsItemSelected() method used this code:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
    

    It creates not a popup menu, but a chooser dialog.

    0 讨论(0)
  • 2021-01-01 20:56

    For AppCompat the changes to 2 styles are needed:

    1)

    <style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
        <item name="android:actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
    </style>
    

    In other words, specifiing attr:actionModeShareDrawable in your MyAppTheme is not what you really need, but it should be mentioned in your attr:actionBarWidgetTheme as following:

    2)

    <style name="Theme.AppCompat.CustomShareIcon" parent="@style/Theme.AppCompat">
        <item name="actionModeShareDrawable">@drawable/abc_ic_menu_share_holo_dark</item> <!-- your icon goes here -->
    </style>
    
    0 讨论(0)
  • 2021-01-01 20:57

    You can override the non-public attribute like this.

    <style name="Theme.MyApp" parent="android:Theme.Holo">
            <item name="*android:actionModeShareDrawalbe">@drawable/icon</item>
    </style>
    

    (*) means reference to the non-public resources in Android

    0 讨论(0)
提交回复
热议问题