【问题标题】:xamarin forms stretch button all over the grid columnxamarin 在整个网格列上形成拉伸按钮
【发布时间】:2017-12-11 15:27:21
【问题描述】:

我希望按钮并排没有空格。

我用网格试过了,但我不知道怎么做。

这是我的代码:

                <StackLayout  HorizontalOptions="FillAndExpand" VerticalOptions="End" Spacing="0" >

                <Label Text="Hello " FontSize="Medium"/>
                <Label FontSize="Small">This sould be a text?</Label>
                <BoxView HeightRequest="1" BackgroundColor="#6fcd11" HorizontalOptions="FillAndExpand" />
                <Grid Padding="0" ColumnSpacing="0" RowSpacing="0" BackgroundColor="Aqua" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions >
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Button Grid.Row="0" Grid.Column="0" Text="Ja"></Button>
                    <Button Grid.Row="0" Grid.Column="1" Text="Nein"></Button>

                </Grid>

            </StackLayout>

感谢您的帮助。

更新:

结果是这样的,但是我想填充它,蓝色背景不可见。我想删除这两个按钮之间的空间。

【问题讨论】:

  • 你的代码没有问题,你确定附上完整的代码吗?
  • 这对我来说在 Android 和 iOS 上都很好用。按钮之间或周围没有空间。可以是样式、自定义渲染器或在 Android 上为 Android 样式。

标签: android ios xaml xamarin xamarin.forms


【解决方案1】:

通过编码,你可以做这样的事情。

new StackLayout
    {
        Spacing = 0,
        Orientation = StackOrientation.Horizontal,
        Children = 
        {
            new Button
            {
                Text = "Ja",
            },
            new Button
            {
                Text = "Nein",

            },

        }
    } 

【讨论】:

  • 设置该 StackLayout 的 margin=0
  • 不,不幸的是它不适用于此边距
【解决方案2】:

你也可以通过让margin参数减号来解决,

试试这个,

<?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:VideoApp"
         x:Class="VideoApp.CallHistoryPage">
<StackLayout VerticalOptions="End" Spacing="0" Padding="0">

    <Label Text="Hello " FontSize="Medium"/>
    <Label FontSize="Small">This sould be a text?</Label>
    <BoxView HeightRequest="1" BackgroundColor="#6fcd11" 
 HorizontalOptions="FillAndExpand" />
    <Grid Padding="0" ColumnSpacing="0" RowSpacing="0" 
BackgroundColor="Aqua">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Button Grid.Row="0" Grid.Column="0" Margin="-5,-7,-5,-6" Text="Ja">
</Button>
        <Button Grid.Row="0" Grid.Column="1" Margin="-5,-7,-5,-6" 
Text="Nein"></Button>

      </Grid>

   </StackLayout>
</ContentPage>

横向模式:

【讨论】:

  • 这太不安全了,不同的版本和分辨率可能会显示不同的结果。
  • 我已经在 3 个具有不同分辨率的不同模拟器上对其进行了测试,包括纵向和横向模式。它在我用代码编写的相同边距下工作正常。
【解决方案3】:

网格的边距应该设置为0,然后效果很好......

【讨论】:

    【解决方案4】:

    这可能是由于您正在渲染的实际底层按钮。 Android 中有 2 个按钮,您可以从 CustomRenderer 继承:

    public class YourButton_Droid : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer 
    
    public class YourButton_Droid : Xamarin.Forms.Platform.Android.ButtonRenderer
    

    我猜现在,您的应用正在渲染 AppCompat.ButtonRenderer。 它有一个内置的阴影,可以在您的情况下创建边距。使用 Android.ButtonRenderer 会让您失去按钮上的 Material Design(触摸响应、阴影等),但边距也可能消失。

    如果您深入研究 android 按钮世界,您也可以使用 AppCompat,通过您的自定义渲染器代码删除边距。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      相关资源
      最近更新 更多