How to remove extra padding from button views in Xamarin Forms for Android?

后端 未结 3 955
执念已碎
执念已碎 2021-01-13 05:12

The button views on Xamarin Forms seem to get extra padding applied when deployed to Android. I have set Resources/values/styles.xml under my Android project to have all spa

相关标签:
3条回答
  • 2021-01-13 05:43

    Well you can use the Device.Runtime platform, described on this link.

    Basically you can choose the type of margin depending on the operating system.

    if(Device.RuntimePlatform == Device.Android)
        button.margin = new Thickness(0);
    

    Is this what you were looking for?

    0 讨论(0)
  • 2021-01-13 05:54

    Try this:

    In your Android Project open your Styles.xml

    Inside the MyTheme.Base style (I am assuming is called like this based on the default Xamarin.Forms template) add the following line:

    <item name="android:buttonStyle">@style/MyButton</item>
    

    Create a new style with name MyButton

    <style name="MyButton" parent="android:Widget.Material.Button">
      <item name="android:layout_margin">0dip</item>
      <item name="android:background">@drawable/button_ripple</item>
     </style>   
    

    In the Drawable folder add a new XML file called button_ripple.xml and paste the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- in drawable folder-->
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight">
    
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="?android:colorAccent" />
            </shape>
        </item>
    
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#ff0000" />
            </shape>
        </item>
    </ripple>
    

    This file is necessary because when the background of a button in Android is changed the ripple effect (the effect when tapping on it) is lost.

    If you don't care about the effect go back to the style file and the MyButton style can be just something like this:

    <style name="MyButton" parent="android:Widget.Material.Button">
      <item name="android:layout_margin">0dip</item>
      <item name="android:background">#ff0000</item>
     </style>
    

    This might help you without CustomRenderers.-

    0 讨论(0)
  • 2021-01-13 05:56

    The Xamarin button now includes a Padding property that can be set to a Thickness.

    Button button = new Button();
    button.Text = "Button";
    button.Clicked += OnClicked;
    button.Padding = new Thickness(0, 0, 0, 0);
    
    0 讨论(0)
提交回复
热议问题