Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6540670
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-02-01 22:54:52

文本和背景的颜色
如您所见,“标签”视图以适合设备的颜色显示文本。 您可以通过设置两个名为TextColor和BackgroundColor的属性来覆盖该行为。 标签本身定义了TextColor,但它从VisualElement继承了BackgroundColor,这意味着Pageand Layout也有一个BackgroundColor属性。
将TextColor和BackgroundColor设置为Color类型的值,这是一个定义了17个用于获取常用颜色的静态字段的结构。 您可以使用上一章中的问候语程序来试验这些属性。 以下是与HorizontalTextAlignment和VerticalTextAlignment结合使用的两种颜色,用于居中文本:

点击(此处)折叠或打开

  1. public class GreetingsPage : ContentPage
  2. {
  3.     public GreetingsPage()
  4.     {
  5.         Content = new Label
  6.         {
  7.             Text = "Greetings, Xamarin.Forms!",
  8.             HorizontalTextAlignment = TextAlignment.Center,
  9.             VerticalTextAlignment = TextAlignment.Center,
  10.             BackgroundColor = Color.Yellow,
  11.             TextColor = Color.Blue
  12.         };
  13.     }
  14. }
结果可能会让你大吃一惊。 正如这些截图所示,标签实际上占据了页面的整个区域(包括在iOS状态栏的下面),并且HorizontalTextAlignment和VerticalTextAlignment属性定位了该区域内的文本:

相比之下,下面是一些使文字颜色相同的代码,而使用HorizontalOptions和VerticalOptions属性居中文本:

点击(此处)折叠或打开

  1. public class GreetingsPage : ContentPage
  2. {
  3.     public GreetingsPage()
  4.     {
  5.         Content = new Label
  6.         {
  7.             Text = "Greetings, Xamarin.Forms!",
  8.             HorizontalOptions = LayoutOptions.Center,
  9.             VerticalOptions = LayoutOptions.Center,
  10.             BackgroundColor = Color.Yellow,
  11.             TextColor = Color.Blue
  12.         };
  13.     }
  14. }
现在标签只占用文本所需的空间,而这正是位于页面中心的位置:

HorizontalOptions和VerticalOptions的默认值不是LayoutOptions.Start,因为文本的默认外观可能会提示。 默认值是LayoutOptions.Fill。 这是导致标签填充页面的设置。 TextAlignment.Start的默认HorizontalTextAlignment和VerticalTextAlignment值是导致文本位于上一章的第一个版本的Greetings程序的左上角的原因。
您可以将不同的效果组合为HorizontalOptions,VerticalOptions,HorizontalTextAlignment和VerticalTextAlignment的各种设置。
您可能想知道:TextColor和BackgroundColor属性的默认值是什么,因为默认值会导致不同平台的颜色不同。
TextColor和BackgroundColor的默认值实际上是一个名为Color.Default的特殊颜色值,它不表示真实颜色,而是用于引用适合特定平台的文本和背景颜色。
让我们更详细地探讨颜色。

阅读(3686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~