[英]How to set the title of action bar in center in Android
Hello I am working on demo app where I need to set the title in ActionBar
in center of it. 您好,我正在开发演示应用程序,需要在其中的ActionBar
中设置标题。 I have tried some posts of SO that did not work for me. 我尝试了一些SO的帖子,这些帖子对我不起作用。 I am trying on Android 4.3 phone. 我正在Android 4.3手机上尝试。
https://stackoverflow.com/a/21770534/2455259 , https://stackoverflow.com/a/19244633/2455259 https://stackoverflow.com/a/21770534/2455259 , https://stackoverflow.com/a/19244633/2455259
but nothing worked for me. 但对我没有任何作用。 Is there any way to set the title of ActionBar
to center in Android using appcompat ? 有什么方法可以使用appcompat将ActionBar
的标题设置为Android的中心吗?
I am doing this like below but still title is in left side. 我这样做如下,但标题仍在左侧。
center_action_bar_title.xml center_action_bar_title.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
In Activity 活动中
TextView customView = (TextView)
LayoutInflater.from(getActivity()).inflate(R.layout.center_action_bar_title,
null);
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
customView.setText(text);
((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(customView, params);
I know this question is very old but I was able to find a solution. 我知道这个问题很老,但是我找到了解决方案。
A solution that will not require you to create a custom action bar. 一种不需要您创建自定义操作栏的解决方案。
private void centerTitle() {
ArrayList<View> textViews = new ArrayList<>();
getWindow().getDecorView().findViewsWithText(textViews, getTitle(), View.FIND_VIEWS_WITH_TEXT);
if(textViews.size() > 0) {
AppCompatTextView appCompatTextView = null;
if(textViews.size() == 1) {
appCompatTextView = (AppCompatTextView) textViews.get(0);
} else {
for(View v : textViews) {
if(v.getParent() instanceof Toolbar) {
appCompatTextView = (AppCompatTextView) v;
break;
}
}
}
if(appCompatTextView != null) {
ViewGroup.LayoutParams params = appCompatTextView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
appCompatTextView.setLayoutParams(params);
appCompatTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
}
}
Then, just call this on your onCreate()
: 然后,只需在onCreate()
上调用它即可:
centerTitle();
That's it. 而已。 That simple. 这么简单。
Hope it helps someone. 希望它可以帮助某人。
I think using custom action bar is the best way to customize it! 我认为使用自定义操作栏是自定义操作的最佳方法! You should at first build your xml file for action bar , here is an example : 首先,您应该为操作栏构建xml文件,这是一个示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_actionbar_relative_layout_main"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/actionbar_background"
android:enabled="false" >
<TextView
android:id="@+id/textviewactivityname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:text="text"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
Then you should get a refrence to this layout using LayoutInflater: 然后,您应该使用LayoutInflater对该布局进行参考:
ViewGroup actionBarLayout = (ViewGroup) youractivity.getLayoutInflater().inflate(R.layout.nameofxmlfile, null);
At the end you should set this as a layout of your actionbar: 最后,您应该将此设置为操作栏的布局:
ActionBar actionBar = youractivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.