如何使机器人的操作栏标题居中对齐? [英] How to make android action bar title center aligned?

查看:144
本文介绍了如何使机器人的操作栏标题居中对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我自己的自定义操作栏获取操作栏标题集中在中间。有效。不过,我加入了选项菜单中的操作栏后,标题再次转移到了左边。我应该怎么做才能得到集中在操作栏中间的称号? 我的code是:

I tried my own Custom Action bar for getting action bar title centered on the middle. It worked. However, after I added the options menu in Action bar, the title again shifted to the left. What should I do in order to get the title centered on the middle of the action bar? my code is:

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);    
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    View cView = getLayoutInflater().inflate(R.layout.custom_actionbar, null);
    actionBar.setCustomView(cView);

和我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" >

<TextView
    android:id="@+id/apptitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:text="@string/app_name" />

推荐答案

我敢肯定,你使用的是下面设置你的自定义主题。

I am sure, you are using the below to set your custom theme.

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);

如果没有,那么请加入第一线。而在自定义布局中的文本居中。 (比重=中心)

If not, then please add the first line. And the text in your custom layout is centered. (Gravity = Center)

此外,我发现了一个类似的联系<一href="http://stackoverflow.com/questions/19244411/center-align-title-in-action-bar-using-styles-in-android">here和<一href="http://stackoverflow.com/questions/19244411/center-align-title-in-action-bar-using-styles-in-android">here.希望这有助于。

Also I found a similar link here and here. Hope this helps.

尝试了这一点为好。

我能够完成我想要的东西通过的没有的显示的标题 的活性,并且代替使用customView如你所说。钥匙 我错过了一段时间了,你必须使用setCustomView(查看, ActionVar.LayoutParams),以获得定制视图方法 居中,简单地设置一个layout_gravity =中心自定义视图 布局文件不起作用。下面是一个简短code片段展示了如何我 得到它的工作:

I was able to accomplish what I wanted by not showing the title of the activity, and instead using a customView as you suggested. The key that I missed for a while was you must use the setCustomView(View, ActionVar.LayoutParams) method in order to get the custom view centered, simply setting a layout_gravity="center" in the custom view layout file does not work. Here's a short code snippet showing how I got it to work:

            TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
            ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

            customView.setText("Some centered text");
            getSupportActionBar().setCustomView(customView, params);

此方法为我工作在至少3.2,2.3和2.2的设备。

This method is working for me on at least 3.2, 2.3, and 2.2 devices.

源代码<一href="https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ">https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

这篇关于如何使机器人的操作栏标题居中对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆