我正在尝试将共享操作添加到工具栏中。工具栏应该是橙色(或像这种情况下的透明),带有白色文本和图标,因此我将此视图用作Toolbar
:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:ignore="UnusedAttribute" />
此外,这是我的应用主题宣言的样子:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
无论我如何改变风格,这都是我得到的:
如何说服ShareActionProvider获取Light
主题?
答案 0 :(得分:3)
我的解决方案基于支持库v7,toolBar,ActionBarActivity,Android Studio
1-删除app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
2-确保您的基本主题为Theme.AppCompat.Light.NoActionBar
3-输入&#34; ShareActionProvider&#34;转到ShareActionProvider的原始代码在代码中的任何地方然后导入v7然后将鼠标对准它然后(按住Ctrl键+左键单击)
4-复制其中的代码并将其粘贴到项目目录中的新java文件中
5-转到您自己的ShareActionProvider并删除此导入如果您拥有import android.support.v7.appcompat.R
6-提供您自己的共享图标,因为默认的是黑色
Drawable myDrawable = mContext.getResources().getDrawable(R.drawable.ic_share);
activityChooserView.setExpandActivityOverflowButtonDrawable(myDrawable);
7-转到您的活动并删除您在步骤3中进行的导入(使用您自己的文件)
8-转到你的onCreateOptionsMenu它应该是这样的:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = new ShareActionProvider(MainActivity.this);
MenuItemCompat.setActionProvider(item , mShareActionProvider);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
shareIntent.setType("text/plain");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
9-最后一步是不要忘记编辑menu.xml
app:actionProviderClass=
"com.yourPackageName.ShareActionProvider" />
答案 1 :(得分:2)
这就是我所做的并且有效。我希望ShareActionProvider
中有白色背景,文字为黑色
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/MainTheme"
app:layout_collapseMode="pin"/>
我的主题
<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/md_white_1000</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#ffffff</item>
<item name="android:textColor">@color/md_black_1000</item>
<item name="listPopupWindowStyle">@style/PopupListStyle</item>
</style>
<style name="PopupListStyle" parent="@style/Widget.AppCompat.Light.ListPopupWindow">
<item name="android:popupBackground">#ffffff</item>
</style>