【问题标题】:Border being ignored on Android - Xamarin.FormsAndroid 上忽略边框 - Xamarin.Forms
【发布时间】:2016-11-29 14:33:31
【问题描述】:

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

请考虑以下代码:

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

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

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

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

建议修改:

请在下面找到我的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>

【问题讨论】:

    标签: android xamarin xamarin.android xamarin.forms


    【解决方案1】:

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

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

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

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

    [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(GenericButtonRenderer))]
    
    namespace Express.CustomRenderers {
        public class GenericButtonRenderer : Xamarin.Forms.Platform.Android.ButtonRenderer { }
    }
    

    【讨论】:

    • 不幸的是,我遇到了完全相同的问题,我最初是透明的,然后将其移至红色,以便屏幕截图更清晰。
    • @Aphire 我假设这是您添加的 Android 主题和/或样式的问题。也许发布您在/values//values-21/ 目录中的Styles.xml 代码。
    • @Aphire 您能否尝试在您的项目中更新到最新版本的 Xamarin Forms 以查看是否有帮助?我确实看到了有关相关问题的一两个错误。
    • 首先,感谢您的坚持,我很感激。其次,我有一个 PCL,所有这些都在 2.3.3.168 上,我相信这是最新发布的稳定版本。
    • @Aphire 不错的发现!没看到那个。我想知道this 人是否在使用材料。可能值得一试。
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 2016-07-15
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    相关资源
    最近更新 更多