Android的:如何在操作栏自定义视图中心一个组成部分? [英] Android: How to center a component in a custom view in action bar?

查看:185
本文介绍了Android的:如何在操作栏自定义视图中心一个组成部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的布局:

<?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="50dp" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/backToScan"
        android:gravity="center"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:textStyle="bold" />

    <ImageView
        android:id="@+id/backToScan"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/back_button" />

</RelativeLayout>

现在我说这successfuly作为操作栏自定义视图。但问题是我不能居中TextView的,我已经使用像Android一样有很多方式:比重=中心,机器人:layout_centerHorizo​​ntal =真,但它不工作

Now I added it successfuly as a custom view for action bar . But the problem is i cannot center the TextView , I have used a lot of way like android:gravity="center" , android:layout_centerHorizontal="true" , but it doesn't work.

它总是漂浮到左边。我怎样才能居中?

It always float to the left. How can I center it?

推荐答案

选项菜单为preventing的的TextView 定心。以下内容添加到您的活动

The options menu is preventing the centering of the TextView. Add the following to your Activity:

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    return false;
}

现在它会工作。

这篇关于Android的:如何在操作栏自定义视图中心一个组成部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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