简体   繁体   English

Android / Xamarin项目中的自定义主题 - 操作栏的中心标题文本

[英]Custom theme in Android / Xamarin project - center Title text of Action Bar

I have custom theme in Android / Xamarin project. 我在Android / Xamarin项目中有自定义主题。 What I can't do is to: 我不能做的是:

  • set header to the middle (and remove icon) 将标题设置为中间(并删除图标)
  • add menu button with menu option (for example button settings) 添加带菜单选项的菜单按钮(例如按钮设置)

I've tried with settings property -> gravity to center but it didn't work. 我尝试过设置属性 - >重力到中心,但它不起作用。

预习

    <?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>

You will have to do it programmatically via a static method inside a static class 您必须通过静态类中的静态方法以编程方式执行此操作

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);
        }

Notice that you have to play with right margin dimension of the text. 请注意,您必须使用文本的右边距维度。 This margin should be equal as width of the home icon(it is there but it is invisible). 此边距应该等于主图标的宽度(它在那里,但它是不可见的)。

You can add the right icon adding it on a Menu.xml and inflating this xml file in the OnCreateOptionsMenu method of your activity. 您可以添加正确的图标,将其添加到Menu.xml上,并在活动的OnCreateOptionsMenu方法中膨胀此xml文件。

You can also do it by using menu and styles xml files. 您也可以使用菜单和样式xml文件来完成。 To add the settings button, for example, you can just add an item in the menu file that defines the menu of the UI shown in the picture. 例如,要添加设置按钮,您只需在菜单文件中添加一个项目,该项目定义图片中显示的UI菜单。 There are a few predefined icons, like the one that i have used (ic_menu_preferences), but you can use another one or use a image instead. 有一些预定义的图标,比如我使用的图标(ic_menu_preferences),但您可以使用另一个图标或使用图像。 So, in, let`s say agrippa_menu.xml: 那么,让我们说agrippa_menu.xml:

 <?xml version="1.0" encoding="utf-8"?>

[...] [...]

<item
    android:id="@+id/menu_item_settings"
    android:title="@string/settings"
    android:icon="@android:drawable/ic_menu_preferences"
    android:showAsAction="ifRoom|withText"
    app:showAsAction="ifRoom|withText"/>

[...] [...]

To ride off the icon you can add this line to the style that defines this UI, or create a new one if you don´t have it already created. 要离开图标,您可以将此行添加到定义此UI的样式,或者如果您尚未创建新行,则创建一个新行。 So, in styles.xml: 所以,在styles.xml中:

<style name="AgrippaTheme" parent="android:Theme.Holo.Light">

   <!--pre v11-->
   <item name="logo">@color/transparent</item>
   <!--post v11-->
   <item name="android:logo">@color/transparent</item>
    <!-- Customize your theme here. -->
</style>

Finally, in reference to put the title in the middle, I haven´t found another way than defining a custom xml action bar and set it programatically like it is said here: Center Align title in action bar using styles in android. 最后,在参考将标题放在中间时,我还没有找到另一种方法,而不是定义一个自定义的xml操作栏,并按照这里所说的方式设置它: 中心在操作栏中使用android中的样式对齐标题。 : . :。 If someone knows how to do it using just styles will be welcomed. 如果有人知道怎么做只使用风格将受到欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM