我在Android / Xamarin项目中有自定义主题。我不能做的是:
我尝试过设置属性 - >引力到中心但它不起作用。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="AgrippaTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/main_orange_color</item>
<item name="android:titleTextStyle">@style/AgrippaTheme.TitleTextStyle</item>
</style>
<!-- ActionBar TitleTextStyle styles -->
<style name="AgrippaTheme.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/main_black_color</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
</resources>
答案 0 :(得分:11)
您必须通过静态类
中的静态方法以编程方式执行此操作public static void SetActionbarText(Activity activity, string text)
{
// Setting custom view enable
activity.ActionBar.SetHomeButtonEnabled(false);
activity.ActionBar.SetIcon(Android.Resource.Color.Transparent);
activity.ActionBar.SetDisplayShowCustomEnabled(true);
activity.ActionBar.Title = "";
LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.SetGravity(GravityFlags.CenterVertical);
LinearLayout.LayoutParams textViewParameters =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
textViewParameters.RightMargin = (int)(40 * activity.Resources.DisplayMetrics.Density);
TextView modelTitle = new TextView(activity);
modelTitle.Text = text;
modelTitle.Gravity = GravityFlags.Center;
linearLayout.AddView(modelTitle,textViewParameters);
ActionBar.LayoutParams actionbarParams =
new ActionBar.LayoutParams(ActionBar.LayoutParams.MatchParent,ActionBar.LayoutParams.MatchParent);
activity.ActionBar.SetCustomView(linearLayout, actionbarParams);
}
请注意,您必须使用文本的右边距维度。此边距应该等于主图标的宽度(它在那里,但它是不可见的)。
您可以添加右侧图标,将其添加到Menu.xml上,并在活动的OnCreateOptionsMenu方法中对此xml文件进行充气。