动作栏中的Android中心自定义视图

尝试为我的操作栏设置自定义视图,但问题是它没有填满操作栏的整个宽度,这使得我可以使对象居中.如何在我的操作栏中设置自定义视图以填满整个操作栏?

设置我的操作栏的代码

actionBar.setdisplayShowHomeEnabled(false);
actionBar.setdisplayShowCustomEnabled(true);
actionBar.setdisplayShowTitleEnabled(false);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setCustomView(inflater.inflate(R.layout.actionbar_layout,null));

还有我的actionbar_layout.xml:

<?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:background="#EDEDED">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:text="Testing"/>
</RelativeLayout>

解决方法

你必须在actionbar_layout.xml中为RelativeLayout设置android:layout_width =“wrap_content”;这与android:layout_gravity =“center”结合使用,允许在ActionBar中居中自定义视图.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...