2016-07-13 60 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); 
     } 
    }