如何使android操作栏标题中心对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21770428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make android action bar title center aligned?
提问by Anu
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);
and my layout is
我的布局是
<?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" />
回答by Atul O Holic
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)
如果没有,请添加第一行。自定义布局中的文本居中。(重力 = 中心)
Also I found a similar link hereand here. Hope this helps.
Try this out as well.
也试试这个。
I was able to accomplish what I wanted by notshowing 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:
通过不显示活动的标题,而是按照您的建议使用 customView,我能够完成我想要的。我错过了一段时间的关键是您必须使用 setCustomView(View, ActionVar.LayoutParams) 方法才能使自定义视图居中,只需在自定义视图布局文件中设置 layout_gravity="center" 是行不通的。这是一个简短的代码片段,展示了我是如何让它工作的:
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);
This method is working for me on at least 3.2, 2.3, and 2.2 devices.
这种方法至少适用于 3.2、2.3 和 2.2 设备。
Source https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ
来源https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ
回答by Willy Chen
First, create a .xml file like this: (action_bar.xml)
首先,创建一个 .xml 文件,如下所示:(action_bar.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#000000" />
</LinearLayout>
And then, in your activity:
然后,在您的活动中:
View view = getLayoutInflater().inflate(R.layout.action_bar, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
Title.setText("Your Title");
getSupportActionBar().setCustomView(view,params);
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
I've tried a lot of solutions and only this one works. Hope it helps you.
我尝试了很多解决方案,只有这个有效。希望对你有帮助。