动作栏上的居中对齐右对齐按钮 [英] Center align right aligned button on Action Bar

查看:102
本文介绍了动作栏上的居中对齐右对齐按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在操作栏上添加了一个按钮,但是现在它像这样最靠右对齐

I have added a button to the Action bar but now it is aligned to the far right like this

我想像这样将按钮对准中心.

I want to align that button to the center like this.

我该如何实现?有没有不用创建自定义操作栏的方法吗?

How can I acheive this? Is there a way to do it without creating a custom Action bar?

这是操作"栏的当前布局.有问题的按钮是第一个<item>.

This is the current layout of the Action bar. The button in question is the first <item>.

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/post_your_notice"
    android:background="@drawable/post_your_notice_button"
    android:orderInCategory="10"
    android:title="Post your notice"
    app:showAsAction="always"
    app:actionLayout="@layout/post_your_notice_button"/>

<item
    android:id="@+id/action_logout"
    android:orderInCategory="100"
    android:title="Log out"
    app:showAsAction="never" />

我通过调用禁用了操作栏上的默认标题

I have disabled the default title on the Action bar by calling

getSupportActionBar().setDisplayShowTitleEnabled(false);

关于我的活动onCreate().

推荐答案

android.support.v7.widget.Toolbar替换操作栏,您可以在其中将Buttonandroid:layout_gravity="center"放置.

Replace actionbar by android.support.v7.widget.Toolbar where you can put Button with android:layout_gravity="center".

添加支持库依赖项compile 'com.android.support:support-v13:25.3.1'

使用布局中的按钮定义工具栏:

Define toolbar with button in your layout:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    ...
    >
    <Button
        android:id="@+id/toolbar_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        ...
        />
</android.support.v7.widget.Toolbar>

在活动中设置工具栏

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Button button = (Button) findViewBydI(R.id.toolbar_button);
    ...
}

这篇关于动作栏上的居中对齐右对齐按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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