如何在xamarin android

时间:2018-04-19 09:30:51

标签: c# xamarin.android

我想在xamarin android中显示编辑文本的下划线颜色。我怎么能这样做。

这是我的代码

_editText = new EditText(Activity);
_editText.SetBackgroundResource(Resource.Drawable.border);
_editText.SetTextColor(_black);
_editText.SetHintTextColor(_black);
_editText.Hint = "Please enter the Title";
_editText.LayoutParameters = new TableRow.LayoutParams(150, TableRow.LayoutParams.FillParent, 1);

1 个答案:

答案 0 :(得分:1)

从Sdk版本21(又名Lollipop)及以上版本中,您应使用BackgroundTinList属性,以获得较低的sdk使用Background.SetColorFilter方法。例如

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        var layout = new LinearLayout(this);
        var editText = new EditText(this)
        {
            LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)           
        };

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            editText.BackgroundTintList = ColorStateList.ValueOf(Color.Green);
        }
        else
        {
            editText.Background.SetColorFilter(Color.Green, PorterDuff.Mode.SrcAtop);
        }

        layout.AddView(editText);

        SetContentView(layout);
    }
}

结果:

enter image description here