2016-07-13 135 views
0

!!!这个问题不是关于创建边框,而是关于创建边框时的长度的问题!条目底部边框

我正在开发一个Xamarin Forms项目,我想在Android设备上的Entry字段下面更改边框的颜色。目前我已经尝试使用自定义渲染器来做到这一点,并且我几乎在那里,但它并不像我想要的那样。蓝色底部边框比输入字段稍宽/更长,但在常规输入字段中,边框和输入字段的宽度/长度相同。如何自定义我的底部边框以适应输入字段的宽度/长度?

图片顶部显示常规输入字段,底部显示带自定义渲染器的输入。

Regular Entry field on top and Entry field with Custom Renderer at the bottom.

下面的代码是在Android的原生创建底部边框的XML。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:top="-2dp" android:left="-2dp" android:right="-2dp"> 
     <shape> 
      <stroke android:color="#33b5e5" android:width="2dp"/> 
     </shape> 
    </item> 
</layer-list> 

下面的代码是自定义呈现为入门

using Xamarin.Forms.Platform.Android; 
using Xamarin.Forms; 
using App.Company; 
using App.Company.Droid; 

[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))] 
namespace App.Company.Droid 
{ 
    class CustomEntryRenderer : EntryRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) 
     { 
      base.OnElementChanged(e); 

      if(Control != null) 
      { 
       Control.SetBackgroundColor(Android.Graphics.Color.Lime); 
       Control.Background = Resources.GetDrawable(Resource.Drawable.BottomBorder, null); 
      } 
     } 
    } 
} 

下面的代码是XAML定义布局

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:App.Company;assembly=App.Company" 
      x:Class="App.Company.Views.StylesTestPage"> 
    <ContentPage.Resources> 
     <ResourceDictionary> 
      <!-- COLORS --> 
      <Color x:Key="Rgray">#A8A8A8</Color> 

      <!-- ENTRIES --> 
      <Style x:Key="entryCustom" 
        TargetType="Entry"> 
       <Setter Property="HorizontalOptions" Value="Center" /> 
       <Setter Property="VerticalOptions" Value="Center" /> 
       <Setter Property="WidthRequest" Value="200" /> 
       <Setter Property="HeightRequest" Value="45" /> 
       <Setter Property="BackgroundColor" Value="Transparent" /> 
      </Style> 
      <Style x:Key="entryCustomGray" 
        TargetType="Entry" 
        BasedOn="{StaticResource entryCustom}"> 
       <Setter Property="TextColor" Value="{StaticResource Rgray}" /> 
      </Style> 
     </ResourceDictionary> 
    </ContentPage.Resources> 
    <ContentPage.Content> 
     <StackLayout Orientation="Vertical"> 
      <Entry Placeholder="Entry placeholder text" 
        Style="{StaticResource entryCustomGray}"> 
      </Entry> 
      <local:CustomEntry Placeholder="In Shared Code" 
           Style="{StaticResource entryCustomGray}"> 
      </local:CustomEntry> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 
+0

的可能的复制[如何改变项在Xamarin.Forms的边框颜色](http://stackoverflow.com/questions/37822668/how-to-change-border-color-of-entry-in -xamarin表单) –

回答

0

您需要以下函数调用OnElementChanged

private void SetBorder(CustomEntry view) 
    { 
     if (view.HasBorder == false) 
     { 
       var shape = new ShapeDrawable(new RectShape()); 
       shape.Paint.Alpha = 0; 
       shape.Paint.SetStyle(Paint.Style.Stroke); 
       Control.SetBackgroundDrawable(shape); 
     } 
     else 
     { 
       Control.SetBackground (originalBackground); 
     } 
    }