【问题标题】:Need to apply a style for getting rid of bottom line on Entry需要应用一种风格来摆脱条目的底线
【发布时间】:2017-10-16 19:28:30
【问题描述】:

首先,我是 Xamarin Forms 的新手,所以请保持温和。我需要摆脱在 Android 中显示 Entry 输入的底线。它在iOS中显示良好。我做了一些研究,发现:

<style name="NoBaseline" parent="android:style/Widget.EditText">
    <item name="android:background">#D3D3D3</item>
</style>

只需将下划线的颜色设置为与输入框的背景颜色相同即可。我已将此代码放在我的styles.xml 文件中,但我觉得我需要应用 这种风格somewhere,但我只是不确定在哪里。对新手的任何帮助将不胜感激。

这是整个文件:

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

    <style name="MyTheme.Base"
           parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#1976D2</item>
        <item name="colorAccent">#FF4081</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    </style>

    <style name="NoBaseline" parent="android:style/Widget.EditText">
        <item name="android:background">#D3D3D3</item>
    </style>

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

    <style name="Splash" parent ="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDisablePreview">true</item>
    </style>
    <style name="MyTheme.Splash" 
           parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_screen</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>

【问题讨论】:

  • 请发布您的整个styles.xml 代码,以便我们看看您是否做对了。默认样式由 Visual Studio 上 xamarin 表单的项目模板自动设置。
  • 好的,我发布了整个文件
  • 我是不是做错了什么???
  • 不是一切都好。我正在搜索是否可以将其放在MyTheme 定义中,但我同意@cvanbeek 的观点,即自定义渲染器是一种更简单的方法
  • 对,我一直在尝试自己解决这个问题。在 MyTheme.Base 样式中添加了这个 -> ​​@style/NoBaseline ,但加载时出现错误“错误膨胀类 android.support.v7。小部件.FitWindowsFrameLayout"

标签: android xamarin.forms multiplatform


【解决方案1】:

如果您在研究时没有看到这个,here is a solution 由 dkudelko 发布到 Github,如果您只是想删除下划线,这可能会更简单一些。

为此,只需在您的 Android 项目中创建一个名为 NoUnderlineEntry 的类,然后添加此代码。

using <YourApp>.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;    

[assembly: ExportRenderer(typeof(Entry), typeof(NoUnderlineEntry))]
namespace <YourApp>.Droid
{
    public class NoUnderlineEntry : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            Control?.SetBackgroundColor(Android.Graphics.Color.Transparent);
        }
    }
}

替换为应用名称后,您将创建一个自定义渲染器,它会覆盖 Android 上的默认 Entry,以将控件背景颜色设置为透明。

Here is documentation 用于为Entry 创建自定义渲染器。

**注意:我没有亲自测试过,但很多人评论说它有效。

【讨论】:

  • 虽然我在多平台上,而不是 Xamarin.Android,所以至少 using 指令不起作用,ExportRenderer 也不起作用。我已经尝试过了,但不知道如何使其适应我的解决方案。
  • 如果你看VS中的解决方案资源管理器,你有多个不同的项目吗?通常在 Xamarin.Forms 中,有一个可移植类库 (PCL),这是您所有共享代码(页面、api 调用等)的位置,然后是各个平台项目(App.Droid、App.iOS 等)。 .您的解决方案中有这些不同的项目吗?
  • 是的,我愿意。我的登录页面在共享部分,我的 styles.xml 在 .Droid 部分
  • 确保将 NoUnderlineEntry 添加到 .Droid 部分。如果 intelisense 显示错误并提出建议,您可能需要添加其他 using 语句。
  • 美丽。有用。非常感谢。我想我只是想把它放到我的共享项目文件夹中,这就是它以前不起作用的原因。真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多