【问题标题】:Entry Custom Renderer(add all borders for Android) in Xamarin.FormsXamarin.Forms 中的条目自定义渲染器(为 Android 添加所有边框)
【发布时间】:2017-08-14 07:30:21
【问题描述】:

我有一个问题。我需要使用 Xamarin.Forms 将所有边框添加到 Android 中的条目。我在 PCL 中创建了渲染器类,并在 xaml 文件中引用了它。然后我在 Android 项目中为渲染器创建了特定的类。 我有这个:

[assembly: ExportRenderer (typeof(EntryCustom), ypeof(EntryRendererCustom))]
namespace InstagramApp.Droid.Renderers
{
 public class EntryRendererCustom : EntryRenderer
 {
    protected override void OnElementChanged(ElementChangedEventArgs<Entry>, e)
    {
        base.OnElementChanged(e);

        if(Control != null)
        {

        }
    }

  }
}

现在我必须在 IF 语句中添加代码,但我是 xamarin 和渲染器的新手。有人能帮我吗?如果有人也可以向我解释一些有关如何使用自定义渲染器的基础知识,那对我来说可能是黄金。谢谢大家!

【问题讨论】:

    标签: android android-edittext xamarin.forms custom-renderer


    【解决方案1】:

    你可以这样做:

    [assembly: ExportRenderer(typeof(Entry), typeof(EntryRendererImplementation))]
    namespace MyProject.Droid.Renderers
    {
        public class EntryRendererImplementation : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.Background = this.Resources.GetDrawable(Resource.Drawable.RoundedCornerEntry);
                    Control.SetPadding(10,10,10,3);
                }
            }
        }
    }
    

    您必须在您的 android 项目中的 Resources/drawable/RoundedCornerEntry.xml 中创建此文件

    <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_focused="true" >
        <shape android:shape="rectangle">
          <gradient
              android:startColor="@color/entry_background"
              android:endColor="@color/entry_background"
              android:angle="270" />
          <stroke
              android:width="1dp"
              android:color="@color/entry_border" />
          <corners
              android:radius="6dp" />
        </shape>
      </item>
      <item>
        <shape android:shape="rectangle">
          <gradient
              android:startColor="@color/entry_background"
              android:endColor="@color/entry_background"
              android:angle="270" />
          <stroke
              android:width="1dp"
              android:color="#c6c6c6" />
          <corners
              android:radius="6dp" />
        </shape>
      </item>
    </selector>
    

    当然,您需要更新您的 Resources/values/colors.xml 文件 类似的东西:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <color name="entry_background">#ffffff</color>
      <color name="entry_border">#BDBDBD</color>
    </resources>
    

    你去!

    【讨论】:

    • 这绝对是我想要的,谢谢!!如果我必须更改占位符样式,例如占位符的字体大小,我必须在 .xml 文件中工作?
    • 作为一个相当新的 Xamarin 开发人员,本教程缺少以下几点... xml 文件必须将构建操作设置为 AndroidResource。这是将文件添加到 Resource.Drawable 类。
    【解决方案2】:

    您在 xamarin 的页面中有很多自定义渲染的示例。

    这是一个很长的解释: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/

    这里是一个混合网络视图的例子:

    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/

    【讨论】:

      【解决方案3】:

      要了解如何使用自定义渲染器,这里有大量的教程和视频,所以最好自己检查一下,

      还有其他方法可以添加边框而不需要使用自定义渲染器,这取决于你说边框时想到的图像是什么

      1. 将条目放在框架内
      2. 将条目放在 boxView 上方,这样您就可以更好地控制“边框”,例如厚度、不透明度甚至动画(也许您希望边框闪烁)

      要执行第二种方法,我会使用这样的网格

      <Grid>
         <BoxView Color="Blue" />
         <Entry/>
      </Grid>
      

      这里的顺序在网格中很重要,对于这种情况,首先定义的元素放在下面。另一个明显的点是BoxView的高度和宽度应该比Enrty的略大,盒子越大边框越厚(我这里还是在说代码;))

      【讨论】:

        猜你喜欢
        • 2020-10-29
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 2017-01-07
        • 2016-05-31
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        相关资源
        最近更新 更多