Xamarin.Forms: remove the registration inside the button

I want to have a small button. The height of the button is 30. On some boards (Android, UWP), the text inside the button is disabled. This is because there are indents between the text and the outer border.

One option is to make the button larger, but there is enough space for the text. Is there an option to install the inner gasket?

+5
source share
3 answers

One option is to use a shortcut instead of a button and use TapGestureRecognizer instead of Click events. A shortcut gives you more control over the layout of the text than Button, but you do not get the advantage that it translates into its own button control on each platform.

+6
source

On Android, you can do this with style.

Resources / values-v21 / styles.xml

<?xml version="1.0" encoding="utf-8" ?> <resources> <style name="myTheme" parent="android:Theme.Material"> <item name="android:buttonStyle">@style/noPaddingButtonStyle</item> </style> <style name="noPaddingButtonStyle" parent="android:Widget.Material.Button"> <item name="android:paddingLeft">0dp</item> <item name="android:paddingRight">0dp</item> </style> </resources> 

AndroidManifest.xml

Set a custom theme as the application theme.

 <application android:label="$safeprojectname$" android:theme="@style/myTheme"></application> 

It sets the left and right margin to 0dp . The default style is indented.

This works on Android> = 5. If you want to support lower versions of Android, you need to add several style folders and use other basic themes like Theme.Holo or Theme.AppCompat .


Or you will go to Renderer:

 using App6.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(Button), typeof(ZeroPaddingRenderer))] namespace App6.Droid { public class ZeroPaddingRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { base.OnElementChanged(e); Control?.SetPadding(0, Control.PaddingTop, 0, Control.PaddingBottom); } } } 
+3
source

Perhaps you can play with FontSize to place the HeightRequest button, unless you need to create your own renderer to install the add-on.

+1
source

Source: https://habr.com/ru/post/1245634/


All Articles