边框在 Android 上被忽略 - Xamarin.Forms [英] Border being ignored on Android - Xamarin.Forms

查看:20
本文介绍了边框在 Android 上被忽略 - Xamarin.Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Xamarin.Forms 构建一个带有绿色边框的圆形按钮,使用的方法我在以前的项目中使用过,方法是向 Button 元素添加 BorderRadius、BorderColour 等值.

I am attempting to build a circular button with a green border using Xamarin.Forms, using a method that I have used on previous projects by adding a BorderRadius, BorderColour etc values to the Button element.

请考虑以下代码:

Button goButton = new Button
{
    Text = "GO!",
    TextColor = Color.White,
    FontSize = 75,
    HeightRequest = 200,
    WidthRequest = 200,
    BorderRadius = 100,
    BorderWidth = 10,
    BorderColor = Color.Green,
    BackgroundColor = Color.Red
};

上面的代码给了我以下不想要的输出:

The above code gives me the following, undesired output:

我尝试在 XAML 中重写并遇到了同样的问题,我还检查了我的 Xamarin(和 NuGet Xamarin.forms 包)版本是否是最新的.然而,在 iOS 版本上,一切看起来都是正确的(我认为这是第一次!)

I have tried re-writing in XAML and had the same issue, I have also checked that my version of Xamarin (and NuGet Xamarin.forms packages) are up to date. However on the iOS version, everything looks correct (first time for everything I suppose!)

在发现问题和解决此问题方面的任何帮助将不胜感激.

Any assistance in finding the problem and fixing this issue would be greatly appreciated.

建议修改:

请在下面找到我的styles.xml

Please find below my styles.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="MainTheme" parent="MainTheme.Base">
    </style>

    <!-- Base theme applied no matter what API -->
    <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

        <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
        <item name="windowNoTitle">true</item>

        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="windowActionBar">false</item>

        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->

        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#2196F3</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>

        <!-- colorAccent is used as the default value for     colorControlActivated
     which is used to tint widgets -->
        <item name="colorAccent">#FF4081</item>

        <!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="windowActionModeOverlay">true</item>

        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    </style>

    <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#FF4081</item>
    </style>

</resources>

推荐答案

Bordered Buttons 对我来说很好用...也许可以尝试一些更合理的值:

Bordered Buttons work fine for me... maybe try some more reasonable values:

Button button = new Button {
    BorderWidth = 1,
    BorderColor = Color.Green,
    BackgroundColor = Color.Transparent,
    Text = "GO!",
    TextColor = Color.White,
}

这就是我的 Button 中的所有内容,边框显示正常.想知道 BackgroundColor = Color.Transparent 是否会对您有所帮助.

That is all I have in my Buttons and the border shows up ok. Wondering if the BackgroundColor = Color.Transparent will make a difference for you.

*添加评论作为答案.根据 this 帖子,为 Button 添加一个完全空的自定义渲染器可能会有所帮助:>

* Adding comment as answer. According to this post, adding a completely empty custom renderer for the Button may help:

[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(GenericButtonRenderer))]

namespace Express.CustomRenderers {
    public class GenericButtonRenderer : Xamarin.Forms.Platform.Android.ButtonRenderer { }
}

这篇关于边框在 Android 上被忽略 - Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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