【发布时间】:2018-07-24 02:22:36
【问题描述】:
我需要删除从 xamarin 表单中点击我的 android 按钮时出现的阴影。 (如上图。当yes和no本来颜色相同。当按下no时,阴影会投射在其上,使其颜色变深)
我遵循了中所述的示例 How to remove the shadow of a button on Xamarin Forms 但由于我的按钮颜色应该是红色,所以我没有将 PCL 中的背景颜色更改为红色。但点击时,阴影仍会出现在 android 按钮上。
然后,我点击了这个链接: https://forums.xamarin.com/discussion/122752/removing-shadow-from-android-buttons 但我按钮上的阴影仍然存在。我认为就像链接中所说的那样,由于我的 xamarin 表单版本是 2.5.0.280555,因此他们提供的解决方案将不起作用。
如果我仍然需要将我的 xamarin 表单版本保持为 2.5.0.280555,如果我需要在点击我的 Xamarin.Forms 项目时删除 android 按钮阴影,是否有人对我可以做些什么有任何建议或解决方案?
这里说该问题已在以后的构建中解决: https://github.com/xamarin/Xamarin.Forms/issues/1954
所以我为 xamarin 表单升级到 2.5.1.52。 但在安卓平台上点击按钮时,我仍然看到按钮上的阴影。
我的代码更改如下: 在 Style.xml 中
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">@style/NoShadowButton</item>
</style>
<style name="NoShadowButton" parent="android:style/Widget.Button">
<item name="android:stateListAnimator">@null</item>
</style>
</resources>
我什至实现了自定义渲染器
[assembly: ExportRenderer(typeof(FlatButton), typeof(UnanimatedButtonRenderer))]
namespace RideNow.Droid
{
public class UnanimatedButtonRenderer : ButtonRenderer
{
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Control != null && this.Element != null)
{
var nativeButton = (Android.Widget.Button)Control;
nativeButton.SetShadowLayer(0, 0, 0, Android.Graphics.Color.Transparent);
nativeButton.Elevation = 0;
if (this.CheckIsCustomFont())
{
var typeFace = this.GetTypeFace();
var typeFaceStyle = this.GetTypeFaceStyle();
if (typeFace != null)
{
nativeButton.SetTypeface(typeFace, typeFaceStyle);
}
}
}
}
}
}
其中 Flatbutton 只是一个自定义按钮,继承自 xamarin 表单的按钮,没有任何附加组件。但似乎没有任何效果
【问题讨论】:
标签: c# android xamarin.forms