2017-09-16 48 views
0

我想显示文本在中心对齐。问题是,一旦我更新文本,它就会被切断。 如果我使用“Fill”作为Horizo​​ntalOptions,文本不会被截断,但文本不在中心。Xamarin.Forms中更新后编辑器中的中心文本

实施例是基于在表格编辑器样品:

public class EditorPageCode : ContentPage 
{ 
    Editor styledEditor = new Editor(); 
    Editor centerText = new Editor(); 
    Editor customEditor = new Editor(); 

    public EditorPageCode() 
    { 
     var layout = new StackLayout { Padding = new Thickness(5, 10) }; 
     this.Title = "Editor Demo - Code"; 
     layout.Children.Add(new Label { Text = "This page demonstrates the Editor View. The Editor is used for collecting text that is expected to take more than one line." }); 
     styledEditor = new Editor 
     { 
      Text = "Xamarin Blue", 
      BackgroundColor = Color.FromHex("#2c3e50"), 
      HeightRequest = 100, 
      HorizontalOptions = LayoutOptions.Center 
     }; 
     customEditor = new Editor { Text = "Default starting text", HorizontalOptions = LayoutOptions.Center }; 
     customEditor.Focused += StyledEntry_Focused; 
     layout.Children.Add(customEditor); 
     centerText = new Editor 
     { 
      IsEnabled = false, 
      Text = "This is a disabled editor", 
      HorizontalOptions = LayoutOptions.Fill 

     }; 
     layout.Children.Add(centerText); 
     this.Content = layout; 
    } 
    void StyledEntry_Focused(object sender, FocusEventArgs e) 
    { 
     var text = centerText.Text + "new "; 
     centerText.Text = text; 
     centerText.HorizontalOptions = LayoutOptions.Center; 
    } 

} 
+0

您是否尝试过使用** FillAndExpand **属性? –

回答

0

我实际上看着来源。由于我不需要在此特定字段上进行编辑,因此可以为我工作的解决方案是使用标签。

 centerText = new Label 
     { 
      HorizontalTextAlignment = TextAlignment.Center, 
      Text = "This is a disabled editor", 
      HorizontalOptions = LayoutOptions.Fill 

     }; 

该标签具有Horizo​​ntalTextAlignment,它在更新后保持文本居中。 编辑器控件没有。