我有一个linearlayout as circle,想要使用值转换器来改变颜色。
下面是我的线性布局看起来
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearLayoutDaysLeft"
android:background="@drawable/RedBackground"
local:MvxBind="BackgroundColor DateColor(EndDate)"/>
注意我已将背景设置为@ drawable / RedBackground
下面是我的@ drawable / RedBackground.xml文件的样子
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<solid android:color="#D00E0D"/>
</shape>
下面是我的DateColorValueConverter代码
protected override Cirrious.CrossCore.UI.MvxColor Convert(object value, object parameter, System.Globalization.CultureInfo culture)
{
var date = (DateTime)value;
int dayLeft;
TimeSpan difference = date - DateTime.Today;
dayLeft = (int)Math.Ceiling(difference.TotalDays);
if (dayLeft < 0)
return (new Cirrious.CrossCore.UI.MvxColor(208, 14, 13, 150));
if (dayLeft >= 0 && dayLeft <= 1)
return (new Cirrious.CrossCore.UI.MvxColor(255, 210, 0, 150));
if (dayLeft > 1)
return (new Cirrious.CrossCore.UI.MvxColor(93, 210, 85, 150));
return (new Cirrious.CrossCore.UI.MvxColor(93, 210, 85, 150));
}
请注意我的DateColor按要求工作,但当它返回颜色时,它会移除毛发形状
请帮帮我
由于
Aaman
答案 0 :(得分:4)
首先感谢斯图尔特指导正确的方向
我已成功完成所需的可绘制背景更改
如果有人需要
,我想与其他人分享以下是我对LinearLayout的自定义绑定
public class ShapeBackgroundBinding : MvxAndroidTargetBinding
{
private readonly LinearLayout _linearLayout;
public ShapeBackgroundBinding(LinearLayout view) : base(view)
{
this._linearLayout = view;
}
protected override void SetValueImpl(object target, object value)
{
// to do logic
}
public override void SetValue(object value)
{
var date = (DateTime)value;
int dayLeft;
TimeSpan difference = date - DateTime.Today;
dayLeft = (int)Math.Ceiling(difference.TotalDays);
if (dayLeft < 0)
_linearLayout.SetBackgroundResource(Resource.Drawable.RedBackground);
else if (dayLeft >= 0 && dayLeft <= 1)
_linearLayout.SetBackgroundResource(Resource.Drawable.YellowBackground);
else if (dayLeft > 1)
_linearLayout.SetBackgroundResource(Resource.Drawable.GreenBackground);
else
_linearLayout.SetBackgroundResource(Resource.Drawable.GreenBackground);
}
public override Type TargetType
{
get { return typeof(DateTime); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneTime; }
}
}
以下是我在设置文件
中注册自定义绑定的方法 protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterFactory(new MvxCustomBindingFactory<LinearLayout>("ShapeBackground", (view) => new ShapeBackgroundBinding(view)));
}
完成上述步骤后,我们只需要绑定线性布局控件,如下面的代码段
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearLayoutTest"
local:MvxBind="ShapeBackground EndDate">
多数民众赞成......
答案 1 :(得分:1)
默认的BackgroundColor绑定在目标视图上使用SetBackgroundColor
- 请参阅https://github.com/MvvmCross/MvvmCross/blob/v3.1/Plugins/Cirrious/Color/Cirrious.MvvmCross.Plugins.Color.Droid/BindingTargets/MvxViewBackgroundColorBinding.cs
如果您想在Drawable中仅对Solid实现一些自定义效果,那么您需要为此形状编写自定义绑定并使用以下技术: - Android: Change Shape Color in runtime - How to change colors of a Drawable in Android? - Change shape solid color at runtime inside Drawable xml used as background
怀疑你可以这样写:
public class ShapeBackgroundColorBinding
: MvxViewColorBinding
{
public ShapeBackgroundColorBinding(View view)
: base(view)
{
}
protected override void SetValueImpl(object target, object value)
{
var view = (View)target;
if (view == null)
return;
var background = view.Background;
background.SetColor((Android.Graphics.Color) value);
}
}
你可以在ShapeBackgroundColor
期间注册为Setup
的绑定 - 尽管这些伪代码可能需要稍微调整才能正常工作!