Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Xamarin.forms。安卓:按钮图像上的按钮外观_Android_Xamarin.ios_Xamarin.android_Xamarin.forms_Imagebutton - Fatal编程技术网

Android Xamarin.forms。安卓:按钮图像上的按钮外观

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

我正在为Android和iOS制作一个应用程序。据我所知,在Android上不可能在imagebuttons上设置边框。这在iOS上是可能的

当我制作一个imagebutton时,它看起来不像Android上的按钮。我找不到办法给它一个按钮看。我只能在带有文本的按钮上找到它

请参阅图像以更好地理解

有人能帮忙吗

这是我的代码(Xamarin和xaml的新代码,所以它可能不是“好看”的代码)



要创建带边框的按钮,您需要创建自定义按钮:

  • 在PCL项目中添加自定义按钮:

    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);
    
            }
        }
    }
    
  • 在Xaml中使用它:

    <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>