Xamarin Border Radius AppCompat [英] Xamarin Border Radius AppCompat
问题描述
在项目中包含AppCompat时,似乎BorderRadius属性不起作用.
It seems that the BorderRadius attribute is not working when including AppCompat in the project.
我试图在此处中创建这样的自定义渲染,但是没有这样做.工作:
I tried to create a custom render like this discussed here, but it didn't work:
namespace Xamarin.Forms
{
public class CustomButton : Button
{
public CustomButton():base()
{
}
protected override void OnParentSet()
{
base.OnParentSet();
}
}
}
在Android项目中:
In the Android project:
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace CalculateurMadelin.Droid.Renderers
{
public class CustomButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
{ }
}
推荐答案
您可以在自定义渲染器中加载Android drawable
以在AppCompat.Button
上定义背景:
You can load an Android drawable
in your custom renderer to define the background on your AppCompat.Button
:
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace AppCompatRender.Droid
{
public class CustomButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
Control.SetBackgroundResource(Resource.Drawable.CustomButtonBackground);
}
}
}
}
添加一个与您在SetBackgroundResource
中使用的名称(即CustomButtonBackground.axml
)匹配的新Resources/Drawable
,在此方法中,我将矩形的角半径设置为10dp
:
Add a new Resources/Drawable
that matches the name you are using in your SetBackgroundResource
(i.e.. CustomButtonBackground.axml
), in this I am setting a corner radius of the rectangle as 10dp
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
</shape>
这篇关于Xamarin Border Radius AppCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!