如何让android动作栏标题中心对齐?

时间:2014-02-14 03:42:47

标签: android android-actionbar center

我尝试了自己的自定义操作栏,让操作栏标题位于中间位置。有效。但是,在我添加操作栏中的选项菜单后,标题再次向左移动。我应该怎么做才能让标题集中在动作栏的中间? 我的代码是:

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);    
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    View cView = getLayoutInflater().inflate(R.layout.custom_actionbar, null);
    actionBar.setCustomView(cView);

我的布局是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" >

<TextView
    android:id="@+id/apptitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:text="@string/app_name" />

2 个答案:

答案 0 :(得分:16)

我确定您使用以下内容设置自定义主题。

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

如果没有,那么请添加第一行。自定义布局中的文本居中。 (重力=中心)

我也找到了类似的链接herehere。希望这会有所帮助。

尝试一下。

我能够完成我想要的而不是显示标题 活动,而是使用您建议的customView。钥匙 我错过了一段时间是你必须使用setCustomView(View, ActionVar.LayoutParams)方法以获取自定义视图 居中,只需在自定义视图中设置layout_gravity =“center”即可 布局文件不起作用。这是一个简短的代码片段,展示了我的情况 让它工作:

            TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
            ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

            customView.setText("Some centered text");
            getSupportActionBar().setCustomView(customView, params);

此方法适用于至少3.2,2.3和2.2设备。

来源https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

答案 1 :(得分:3)

首先,创建一个.xml文件,如下所示:(action_bar.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:orientation="horizontal">

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:maxLines="1"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:textStyle="bold"
    android:textSize="18sp"
    android:textColor="#000000" />

</LinearLayout>

然后,在你的活动中:

View view = getLayoutInflater().inflate(R.layout.action_bar, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
    Title.setText("Your Title");

getSupportActionBar().setCustomView(view,params);       
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title

我尝试了很多解决方案,只有这个有效。希望它可以帮到你。

相关问题