2016-07-13 7 views
0

!!! Bei dieser Frage geht es nicht darum, den Rahmen zu erstellen, sondern um die Länge des Rahmens zu bestimmen, wenn er erstellt wurde !!!Untere Grenze am Eintrag

Ich arbeite an einem Xamarin Forms-Projekt, wo ich die Farbe des Rahmens unter dem Eingabefeld auf Android-Geräten ändern möchte. Derzeit habe ich versucht, dies mit einem benutzerdefinierten Renderer zu tun, und ich bin fast da, aber es scheint nicht so, wie ich es möchte. Der blaue untere Rand ist etwas breiter/länger als das Eingabefeld, aber im regulären Eingabefeld haben der Rahmen und das Eingabefeld die gleiche Breite/Länge. Wie kann ich meinen unteren Rand an die Breite/Länge des Eingabefeldes anpassen?

Das Bild zeigt das reguläre Eingabefeld oben und den Eintrag mit benutzerdefiniertem Renderer ganz unten.

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

Der folgende Code ist die XML für den unteren Rand nativ in Android zu schaffen.

<?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> 

Der folgende Code ist das Custom Renderer für den Eintrag

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); 
      } 
     } 
    } 
} 

Der folgende Code wird der XAML definieren das Layout

<?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

Mögliches Duplikat [Wie Randfarbe des Eintrags in Xamarin.Forms ändern] (http://stackoverflow.com/questions/37822668/how-to-change-border-color-of-entry-in -xamarin-Formen) –

Antwort

0

Sie müssen in unten Funktion aufzurufen 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); 
     } 
    } 
Verwandte Themen