【发布时间】:2016-12-06 11:15:30
【问题描述】:
我正在启动我的第一个 Xamarin Forms 应用程序 - 目前只是 Hello World - 但是我需要能够将标题栏中的文本居中。我正在使用一个 MasterDetailPage,它有一个 ContentPage 作为 Master,一个 NavigationPage 作为 Detail。从下面的屏幕截图中,我希望能够将“主页”居中。
我已经尝试过像Changing Actionbar Tab Text Size in AppCompact Theme 的答案一样的标记,但似乎没有任何影响文本的位置。我也尝试了自定义渲染器,但是没有用。
有人能做到吗?
用一些代码更新我的问题。我的 MasterDeutil.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,我已经用一些源代码更新了这个问题
-
@YuraBabiy,感谢您的回复-但是我已经阅读了该帖子,但没有帮助
-
这个案子你解决了吗?我已经调查了这个问题几天,只有我可以有一个解决方法,而且这个方法对我来说不是很优雅......我现在面临的最大问题是我无法获得渲染的
ToolBar' s context,我用的是MainActivity作为它的context,但是好像打错了。
标签: xamarin xamarin.android xamarin.forms xamarin.forms-styles