Android Xamarin.forms。安卓:按钮图像上的按钮外观
我正在为Android和iOS制作一个应用程序。据我所知,在Android上不可能在imagebuttons上设置边框。这在iOS上是可能的 当我制作一个imagebutton时,它看起来不像Android上的按钮。我找不到办法给它一个按钮看。我只能在带有文本的按钮上找到它 请参阅图像以更好地理解 有人能帮忙吗 这是我的代码(Xamarin和xaml的新代码,所以它可能不是“好看”的代码)Android Xamarin.forms。安卓:按钮图像上的按钮外观,android,xamarin.ios,xamarin.android,xamarin.forms,imagebutton,Android,Xamarin.ios,Xamarin.android,Xamarin.forms,Imagebutton,我正在为Android和iOS制作一个应用程序。据我所知,在Android上不可能在imagebuttons上设置边框。这在iOS上是可能的 当我制作一个imagebutton时,它看起来不像Android上的按钮。我找不到办法给它一个按钮看。我只能在带有文本的按钮上找到它 请参阅图像以更好地理解 有人能帮忙吗 这是我的代码(Xamarin和xaml的新代码,所以它可能不是“好看”的代码) 要创建带边框的按钮,您需要创建自定义按钮: 在PCL项目中添加自定义按钮: public class
要创建带边框的按钮,您需要创建自定义按钮:
public class ButtonWithBorder:Button
{
}
[assembly: ExportRenderer(typeof(ButtonWithBorder),typeof(ButtonWithBorderRenderer))]
namespace ButtonWithBorderDemo.Droid
{
public class ButtonWithBorderRenderer:ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
Control.Background=ContextCompat.GetDrawable(this.Context, Resource.Drawable.button_bg);
}
}
}
<StackLayout
VerticalOptions="Center">
<local:ButtonWithBorder Clicked="ButtonWithBorder_Clicked">
<local:ButtonWithBorder.Image>
<OnPlatform x:TypeArguments="FileImageSource"
Android="gluehbirne.png"
iOS="Pay_MobilePaylogo2.png"/>
</local:ButtonWithBorder.Image>
</local:ButtonWithBorder>
</StackLayout>
按钮\u bg.axml
添加到Resources\drawable
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>