2017-08-14 32 views
0

我想做一個圖像的按鈕,並且每個按鈕都有一個文本里面...但我不太瞭解xamarin形式,我是一個初學者 而我的圖像是小的,而文字比它大...如何做圖像填充文本 - 總是相同的大小 - xamarin.forms

我正在使用一個網格...並在它內部一個其他網格與1 colunm和1行每按鈕(圖像和文本) 我NEET圖像(這是我的按鈕)獲取的是文本的大小裏面

enter image description here

我此刻的xml:

<!-- Buttons grid--> 
    <Grid HorizontalOptions="Center" Margin="20,20,20,20" RowSpacing="30" ColumnSpacing="10" BackgroundColor="Aquamarine" VerticalOptions="FillAndExpand"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

    <!--One of my buttons grid, there are 4 others as this one--> 
<Grid Grid.Column="0" Grid.Row="1" BackgroundColor="Gray"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 

      <Image Source="comochegarbtn.png" HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="0"> 
       <Image.GestureRecognizers> 
        <TapGestureRecognizer Tapped="MapaClique"/> 
       </Image.GestureRecognizers> 
      </Image> 

      <StackLayout Grid.Row="0" Grid.Column="0" HorizontalOptions="Center" VerticalOptions="Center"> 
       <Image Source="Location.png" VerticalOptions="FillAndExpand"/> 
       <Label Text="Como Chegar" TextColor="White" HorizontalOptions="Center" Style="{Binding labelsfont}"/> 
      </StackLayout> 
      </Grid> 
     </Grid> 

在Android中,我可以解決,使用WRAP_CONTENT 但是在這裏,在xamarin形式,我不知道如何解決它 灰色部分是網格項目大小

+0

您是否需要根據圖像寬度或根據文本大小拉伸圖像來包裝文本? –

回答

0

如果您需要文本的確切大小作爲圖像的寬度,那麼您將不得不使用RelativeLayout

RelativeLayout的圖像寬度使用RelativeToView。 這與Android中的相對佈局類似。

<RelativeLayout> 

      <Image RelativeLayout.WidthConstraint="{ConstraintExpression Type=Type=RelativeToView, ElementName=myStacklayout,Property=Width,Factor=1}" Source="comochegarbtn.png" HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="0"> 
       <Image.GestureRecognizers> 
        <TapGestureRecognizer Tapped="MapaClique"/> 
       </Image.GestureRecognizers> 
      </Image> 

      <StackLayout x:Name="myStacklayout" HorizontalOptions="Center" VerticalOptions="Center"> 
       <Image Source="Location.png" VerticalOptions="FillAndExpand"/> 
       <Label Text="Como Chegar" TextColor="White" HorizontalOptions="Center" Style="{Binding labelsfont}"/> 
      </StackLayout> 
</RelativeLayout> 

使用一個或兩個相對佈局不會對您造成太大影響。但是,如果你認爲它可以編寫自定義渲染並在本地層創建一個複雜的按鈕。

+0

1.賈森史密斯不推薦相對佈局。 2.如果您使用相對查看,您如何知道屏幕上圖像的大小?請添加一些代碼,使這個答案有用 –

+0

不要誤會我的意思,我喜歡相對佈局:-)但代碼仍然需要答案:-) –

+0

hmmmmm 我試過了......但我不確定關於它 在相對佈局我有類型= RelativeToParent, 屬性=身高, 因子= 0.5, 常數= -100 如果在屬性我使用HEIGHT 然後視圖的高度將是相對於親本高度? 和因素和常數?他們在做什麼? –

相關問題