如何在动作条的默认主题中心对齐标题(Theme.Holo.Light) [英] How to align title at center of ActionBar in default theme(Theme.Holo.Light)

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

问题描述

我搜索了很多,但能不能找到一种方法,我该如何设置,那么标题动作条的中心,而不是左对齐。我用低于code设置在中心标题:

I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :

ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView();
LinearLayout root= (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer= (FrameLayout) root.getChildAt(0);
TextView title= (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);

但它给错误如下:

But it gives error as below :

ClassCastException : com.android.internal.widget.ActionBarView can not 
be cast to android.widget.TextView.

任何其他解决办法..Any帮助将AP preciated。

Any other solution ..Any help will be appreciated.

推荐答案

您可以创建一个自定义布局,并将其应用到动作条。

You can create a custom layout and apply it to the actionBar.

要做到这一点,遵循这些2简单的步骤:

To do so, follow those 2 simple steps:

1。 Java的code

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

其中, R.layout.actionbar 是下面的XML。

2。 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:layout_gravity="center"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="YOUR ACTIVITY TITLE"
    android:textColor="#ffffff"
    android:textSize="24sp" />
</LinearLayout>

它可以是只要你想的复杂。试试吧!

It can be as complex as you want. Try it out!

编辑:

有关背景您可以使用属性安卓背景在容器布局(LinearLayout中在这种情况下)。您可能需要设置安卓layout_height =WRAP_CONTENT match_parent

For background you can use property android:background in the container layout (LinearLayout in that case). You may need to set android:layout_height="wrap_content"to match_parent.

您也可以把一个 LOGO / 图标就可以了。要做到这一点,你必须简单地添加一个的ImageView 您的布局中,并设置布局属性安卓方向来水平(或简单地用一个RelativeLayout的并管理它自己)。

You can also put a LOGO / ICON on it. To do so, you have to simply add an ImageView inside your layout, and set layout property android:orientation to horizontal (or simply use a RelativeLayout and manage it yourself).

这篇关于如何在动作条的默认主题中心对齐标题(Theme.Holo.Light)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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