2015-09-04 52 views
3

我希望iOS和Android上的按鈕文字顏色相同。所以我想我只是將代碼中的TextColor屬性設置爲我想要的顏色。在iOS上,禁用的文本顏色仍然是灰色的(這是我想要的),但是,在Android上,禁用的文本顏色與啓用的文本顏色相同。如何在Android灰色上設置禁用的文本顏色(或者默認爲禁用按鈕的顏色)? 謝謝Xamarin表單按鈕TextColor在Android上的表現與iOS相比有所不同

+0

使用自定義渲染器:https://stackoverflow.com/a/49193488/1039935 – Arvis

回答

7

這是Xamarin.Forms中的默認行爲。若要解決,實現了自定義顏色,您可以:

  • 使用觸發器來改變從啓用已禁用去,反之亦然當顏色
  • 手動設置顏色啓用和禁用按鈕
  • 時使用自定義渲染

因爲觸發器是實現你想要什麼的最簡單,一致的方式,我只覆蓋。有關自定義渲染器的更多信息,請參閱xamarin docs

使用觸發器時,您可以在未啓用時動態地將字體顏色更改爲灰色。有關觸發器的更多信息,請參見docs

<Button Text="I am a Button"> 
<Button.Triggers> 
    <Trigger TargetType="Button" 
     Property="IsEnabled" Value="true"> 
     <Setter Property="TextColor" Value="Red" /> 
     <!-- red is the desired color when the button is enabled.--> 
    </Trigger> 
    <Trigger TargetType="Button" 
     Property="IsEnabled" Value="false"> 
     <Setter Property="TextColor" Value="Gray" /> 
     <!-- gray is the desired color when the button is disabled.--> 
    </Trigger> 
</Button.Triggers> 
</Button> 
+0

這很好。非常感謝你。 –

+1

其不工作.. –

+0

它不適合我。任何更新? – Sudha

0

如果上述方法不爲你工作,你可以嘗試檢查this workaround。它使用自定義渲染器和NSAttributedString設置標題在禁用狀態

相關問題