如何中心对齐在Android上的动作条的标题? [英] How to center align the ActionBar title in Android?

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

问题描述

我尝试使用下面的code居中文本中的动作条,但赞同的左边。

你怎么让它出现在市中心?

 动作条动作条= getActionBar();
actionBar.setDisplayShowTitleEnabled(真正的);
actionBar.setTitle(食堂家);
actionBar.setHomeButtonEnabled(真正的);
actionBar.setIcon(R.drawable.back);
 

解决方案

要存档具有ABS居中标题(如果你想拥有这个默认动作条,只是删除了支持的方法名),你可能只是做到这一点:

在你的活动,在你的的onCreate()方法:

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

abs_layout

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =中心
        机器人:文本=我的标题
        机器人:文字颜色=#FFFFFF
        机器人:ID =@ + ID / MYTEXT
        机器人:TEXTSIZE =18sp/>

< / LinearLayout中>
 

现在你应该有一个动作条只是一个头衔。如果您想设置一个自定义背景,在上面的布局设置(但不要忘记设置安卓layout_height =match_parent)。

<$p$p><$c$c>getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));

I am trying to use the following code to center the text in the ActionBar, but it aligns itself to the left.

How do you make it appear in the center?

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("Canteen Home");
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.back);

解决方案

To archive having a centered title in ABS (if you want to have this in the default ActionBar, just remove the "support" in the method names), you could just do this:

In your Activity, in your onCreate() method:

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

abs_layout:

<?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="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="my Title"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</LinearLayout>

Now you should have an Actionbar with just a title. If you want to set a custom background, set it in the Layout above (but then don't forget to set android:layout_height="match_parent").

or with:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));

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

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