简体   繁体   English

从 Xamarin Forms 在 Android 上制作混合大小写的按钮

[英]Making buttons mixed case on Android from Xamarin Forms

I am new to Xamrin.我是 Xamrin 的新手。 I am attempting to build a Xamarin Forms app (as little native as possible) for running on both Android and IOS.我正在尝试构建一个 Xamarin Forms 应用程序(尽可能少的原生),以便在 Android 和 IOS 上运行。

I am running in the Android Emulator and by buttons all have uppercase text even though I'm specifying text in mixed case.我在 Android Emulator 中运行,并且按钮都有大写文本,即使我指定大小写混合的文本。

I have found a number of pages that say to add the following to styles.xml:我发现许多页面都说要将以下内容添加到styles.xml:

<item name="android:textAllCaps">false</item>

and another that referenced:另一个引用:

<item name="textAllCaps">false</item>

It looks like, though, that there's been some changes according to the docs in styles.xml, so I added it and it now looks like this:不过,根据styles.xml 中的文档,看起来有些变化,所以我添加了它,现在看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
    <!-- As of Xamarin.Forms 4.6 the theme has moved into the Forms binary -->
    <!-- If you want to override anything you can do that here. -->
    <!-- Underneath are a couple of entries to get you started. -->

    <!-- Set theme colors from https://aka.ms/material-colors -->
    <!-- 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>-->
    <item name="textAllCaps">false</item>
    <item name="android:textAllCaps">false</item>
  </style>
</resources>

But when I run the emulator, it shows all button text in all uppercase.但是当我运行模拟器时,它以大写形式显示所有按钮文本。

I don't know what else to try.我不知道还能尝试什么。

You can create the Custom Renderer for button and set the SetAllCaps to false and try.您可以为按钮创建自定义渲染器并将SetAllCaps设置为false并尝试。

Here is the Code:这是代码:

public class CustomButtonRenderer : ButtonRenderer
{
    public CustomButtonRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            // Cleanup
        }

        if (e.NewElement != null)
        {
           var button = (Button)this.Control; 
           button.SetAllCaps(false);
        }
    }
}

I made a test and found the issue is related to the version of Xamarin.Forms .我做了一个测试,发现问题与Xamarin.Forms的版本有关。

  • If you are using Xamarin.Forms 4.6 , the solution you mentioned before does the trick , we just need to add <item name="android:textAllCaps">false</item> into style.xml .如果您使用的是 Xamarin.Forms 4.6,您之前提到的解决方案可以解决问题,我们只需要将<item name="android:textAllCaps">false</item>style.xml

  • If you're using the latest version(4.8) , the solution above does not work , we need to create custom renderer for the Button , pls copy the code into your android project.如果您使用的是最新版本(4.8),上述解决方案不起作用,我们需要为 Button 创建自定义渲染器,请将代码复制到您的 android 项目中。

     [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(MyRenderer))] namespace YourNameSpace.Droid { class MyRenderer : ButtonRenderer { public MyRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) { base.OnElementChanged(e); if (e.OldElement != null) { // Cleanup } if (e.NewElement != null) { Control.SetAllCaps(false); } } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM