Xamarin.Forms:删除Button内的填充 [英] Xamarin.Forms: Remove padding inside Button

查看:177
本文介绍了Xamarin.Forms:删除Button内的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个小按钮.按钮的高度为30.在某些平台(Android,UWP)上,按钮内的文本被切除.这是因为文本和外部边框之间存在填充.

I want to have a small button. The height of the button is 30. On some plattforms (Android, UWP) the text inside the button is cut off. That's because there is a padding between the text and the outside border.

一种选择是使按钮变大,但是有足够的空间容纳文本.有设置内部填充的选项吗?

One option is to make the button bigger, but there is enough space for the text. Is there an option for setting the inside padding?

推荐答案

在Android上,您可以通过样式进行设置.

On Android, you can do this with styling.

资源/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 custom theme as application theme.

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

将左右边距设置为0dp.默认样式带有填充.

It's setting the left and right margin to 0dp. The default style has a padding.

这适用于Android> =5.如果要支持较低的android版本,则必须添加多个样式文件夹并使用其他基本主题,例如Theme.HoloTheme.AppCompat.

This is working on Android >= 5. If you want to support lower android versions, you have to add multiple style folders and use other base themes like Theme.Holo or Theme.AppCompat.

或者您选择渲染器:

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);
        }
    }
}

这篇关于Xamarin.Forms:删除Button内的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆