【问题标题】:Set Label text to non-transparent将标签文本设置为不透明
【发布时间】:2020-10-27 21:21:34
【问题描述】:

我有一个带有标签的网格,每个“平铺”都有透明的背景颜色,但文本也似乎是透明的。有没有办法去除标签文本的透明度?

我的代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LoyaltyWorx.GridMenu"
             BackgroundImage="grid.jpg"
             Title="Main Menu"
             >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Label Text="Cards" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="0" Grid.Column="0" BackgroundColor="#1481BA"  Opacity="0.5" x:Name="CardTile"/>
        <Label Text="Transactions" TextColor="Black" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="0" Grid.Column="1" BackgroundColor="#ede890" Opacity="0.5" x:Name="TransactionTile"/>
        <Label Text="Promotions" TextColor="White" FontSize="30" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="1" Grid.Column="0" BackgroundColor="#1481BA"  Grid.ColumnSpan="2" Opacity="0.7" x:Name="PromoTile"/>
        <Label Text="Settings" TextColor="Black" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="2" Grid.Column="0" BackgroundColor="#ede890" Opacity="0.5" x:Name="SettingsTile" />
        <Label Text="My Profile" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Grid.Row="2" Grid.Column="1" BackgroundColor="#1481BA" Opacity="0.5" x:Name="ProfileTile"/>
    </Grid>
</ContentPage>

截图:

【问题讨论】:

  • 您可能需要为背景颜色创建一个Label(或者可能是BoxViewFrame),然后创建一个具有透明背景但不是文本的Label,因此文本会清楚地显示出来

标签: xaml xamarin.forms


【解决方案1】:

您需要在StackLayout 中添加您的标签,如下所示:

<StackLayout Grid.Row="0" Grid.Column="0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="#1481BA" Opacity="0.5" >
    <Label Text="Cards" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="Transparent" x:Name="CardTile"/>
</StackLayout>

您也可以使用Frame 代替StackLayout

【讨论】:

    【解决方案2】:

    在标签上,将 BackgroundColor 属性设置为 0.5 alpha (128 = 0x80) 并将不透明度保持为 1.0。

      <Label ... BackgroundColor="#801481BA"  Opacity="1.0" />
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 2015-02-03
      • 2011-02-21
      • 1970-01-01
      • 2011-09-01
      • 2016-12-14
      • 2011-08-05
      • 1970-01-01
      相关资源
      最近更新 更多