我正在开发一个mvvm light xamarin表单应用程序,在该应用程序中,我允许用户在应用程序本身中创建自己的主题。到目前为止,除了发送按钮(图像图标),添加按钮(图像图标)和汉堡菜单图标之外,我已经可以按预期进行所有工作。
用户选择一个图像/名称,然后将来自该颜色的十六进制值应用于控件/字体。但是我在尝试以编程方式设置汉堡菜单颜色时遇到了问题。我从android方面开始。我可以一次使用styles.xml设置颜色。
但这还不足以实现我的目标。我还没有弄清楚如何获取对切换抽屉的引用来更改颜色,而无需创建新的引用并提供可绘制对象。
我还需要在iOS(可更改颜色汉堡包菜单)上执行相同的操作。我不想提供图像,因为用户将根据十六进制值设置颜色...我最好的选择是什么?在这一点上,我什至想做些什么?
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected ActionBarDrawerToggle drawerToggle;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
}
}
我在这里找到此代码:
drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
这将是理想的,但是我没有对抽屉Toggle的引用...(该对象为null)
我还尝试使用styles.xml进行设置,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/green</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
此代码确实可以设置一次,但是我需要通过单击按钮在应用程序中将其重置。因此,这对于我要执行的操作是不够的。
在您希望更改的地方添加代码。
var工具栏= FindViewById(Resource.Id.toolbar); var view =(AppCompatImageButton)toolbar.GetChildAt(2); // 1. actionmenuview(主菜单/辅助菜单图标),// 2. appcompattextview(标题),// 3. appcompatimagebutton(汉堡图标)view.SetImageDrawable(d); // view.SetImageDrawable(d);
就我而言,我有菜单图标,所以我选择GetChildAt(2)[第三项]