如何在Android的中心设置操作栏的标题 [英] How to set the title of action bar in center in Android

查看:96
本文介绍了如何在Android的中心设置操作栏的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发演示应用程序,需要在其中心的ActionBar中设置标题.我尝试了一些SO的帖子,这些帖子对我不起作用.我正在尝试使用Android 4.3手机.

Hello I am working on demo app where I need to set the title in ActionBar in center of it. I have tried some posts of SO that did not work for me. I am trying on Android 4.3 phone.

https://stackoverflow.com/a/21770534/2455259 https://stackoverflow.com/a/19244633/2455259

但是对我没有任何帮助.有什么方法可以使用appcompat将ActionBar的标题设置为在Android中居中?

but nothing worked for me. Is there any way to set the title of ActionBar to center in Android using appcompat ?

我正在像下面这样操作,但标题仍在左侧.

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" />

活动中

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

推荐答案

我认为使用自定义操作栏是自定义操作的最佳方法! 首先,您应该为操作栏构建xml文件,这是一个示例:

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

然后,您应该使用LayoutInflater对该布局进行参考:

Then you should get a refrence to this layout using 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);

这篇关于如何在Android的中心设置操作栏的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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