我開始了我的第一個Xamarin Forms應用程序 - 目前只比Hello World多 - 但是我需要能夠將標題欄中的文本居中。我使用的MasterDetailPage有一個ContentPage作爲主和一個NavigationPage作爲細節。從下面的屏幕截圖中,我希望能夠將「主頁」居中。Xamarin Forms MasterDetail中心標題
我試過標記,就像Changing Actionbar Tab Text Size in AppCompact Theme的答案,但是沒有任何東西似乎影響文本的位置。我也試過了一個自定義渲染器,但是沒有奏效。
有沒有人能夠做到這一點?
用一些代碼更新我的問題。我MasterDeatil.cs:
public class MasterDetail : MasterDetailPage
{
Master master;
public MasterDetail()
{
master = new Master();
Master = master;
Detail = new NavigationPage(new Home());
master.ListView.ItemSelected += ListView_ItemSelected;
ToolbarItem cart = new ToolbarItem()
{
Text = "Test",
Icon = "shoppingcart.png"
};
cart.Clicked += async (object sender, System.EventArgs e) =>
{
await DisplayAlert("Done", "Msg", "Ok", "Can");
};
ToolbarItems.Add(cart);
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
MasterItem item = e.SelectedItem as MasterItem;
if (item != null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
master.ListView.SelectedItem = null;
IsPresented = false;
}
}
}
代碼Master.cs:
public class MasterDetail : MasterDetailPage
{
Master master;
public MasterDetail()
{
master = new Master();
Master = master;
Detail = new NavigationPage(new Home());
master.ListView.ItemSelected += ListView_ItemSelected;
ToolbarItem cart = new ToolbarItem()
{
Text = "Test",
Icon = "shoppingcart.png"
};
cart.Clicked += async (object sender, System.EventArgs e) =>
{
await DisplayAlert("Done", "Msg", "Ok", "Can");
};
ToolbarItems.Add(cart);
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
MasterItem item = e.SelectedItem as MasterItem;
if (item != null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
master.ListView.SelectedItem = null;
IsPresented = false;
}
}
}
代碼Detail.cs:
public class Detail : ContentPage
{
public Detail()
{
Title = "Shaves2U";
Content = new StackLayout
{
Children = {
new Label {
Text = "Detail data goes here",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
}
}
};
}
}
style.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base theme applied no matter what API -->
<style name="MainTheme" parent="Theme.AppCompat">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
我已經嘗試了以下style.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base theme applied no matter what API -->
<style name="MainTheme" parent="Theme.AppCompat">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="android:actionBarTabTextStyle">@style/CustomTab</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
<style name="CustomTab" parent="@android:style/Widget.AppCompat.ActionBar.TabText">
<item name="android:gravity">center</item>
</style>
</resources>
我希望我剛剛拿到了XML錯誤
你能否包含一些如何構建你的'MasterDetailPage'的代碼?只需附上您的問題的截圖,我們很難知道我們如何爲您提供幫助。另外,請注意'Xamarin.Forms'使用相應平臺的默認行爲。當涉及到Android的主細節頁面的標題時,標題通常始終保持對齊。 – Demitrian
@Demitrian,我用一些源代碼更新了這個問題 – markpirvine
看一下這個: https://forums.xamarin.com/discussion/59722/toolbar-title-set-center-align –