使用样式在Android的中心对齐标题在行动吧。 [英] Center Align title in action bar using styles in android.

查看:265
本文介绍了使用样式在Android的中心对齐标题在行动吧。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在开发Android应用程序。在我的应用程序,我使用操作栏。我想使中心对准我的窗口标题行动吧。我知道这是可能使用XML文件。但我想用风格来做到这一点,这样它会适用于我的每一个窗口,如何做到这一点需要帮助。谢谢你。

解决方案
  

我知道这是可能使用XML文件。但我想用风格来做到这一点   因此,这将适用于我的每一个窗口,如何做到这一点需要   帮助。

您仍然可以使用XML做到这一点。稍稍偏出,而不是应用该样式的应用程序只是一个单一的活动。请看到Android文档的完整说明:

https://developer.android.com/training/basics/actionbar/ styling.html

那么你的主题应用到整个应用程序或个人活动:

 <应用机器人:主题=@风格/ CustomActionBarTheme... />
 

编辑:下面是你必须处理的标题居中:

在你action_bar.xml你有这样的事情:

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

<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:文本=一些TITLE
    机器人:TEXTSIZE =18sp/>
< / RelativeLayout的>
 

然后在您的onCreate(),你会碰到这样的:

  getSupportActionBar()setCustomView(R.layout.action_bar)。
 

如果你需要有这样的每一个活动,你有两个选择:

  1. 的强制方法:把code上面一行在每一个活动的onCreate
  2. 扩展活动,把你的动作条初始化code在OnCreate。然后扩展您的自定义Activity类为每个活动。只是不要忘了调用超()中的每个活动的onCreate()。

Hi I am developing android application. In my application I am using action bar. I want to make center align my title of window in action-bar. I know it is possible using xml files. but I want to do it using style so that it will applicable for my every window, how to do this need help. Thank you.

解决方案

I know it is possible using xml files. but I want to do it using style so that it will applicable for my every window, how to do this need help.

You still use xml to do this. Just apply the style application wide and not just to a single activity. Please see the android documentation for a complete description:

https://developer.android.com/training/basics/actionbar/styling.html

Then apply your theme to your entire app or individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />

EDIT: Here's how you'll have to handle the centering of the the title:

In your action_bar.xml you'll have something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_centerHorizontal="true"
    android:text="SOME TITLE"
    android:textSize="18sp" />
</RelativeLayout>

Then in your onCreate() you'll have something like:

getSupportActionBar().setCustomView(R.layout.action_bar);

If you need to have this in every activity, you have 2 choices:

  1. The brute force way: Put the above line of code in every Activity's onCreate.
  2. Extend Activity and put your ActionBar initialization code in the onCreate. Then Extend your custom Activity class for each Activity. Just don't forget to call the super() in each Activity's onCreate().

这篇关于使用样式在Android的中心对齐标题在行动吧。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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