繁体   English   中英

如何在Android的操作栏中对齐中心项

[英]How to align center item in an Action bar on Android

我有一个带有3个菜单项的操作栏,可悲的是,这些菜单项在左侧对齐,我将在中央对齐。 我一直在环顾四周,尝试了一些东西,但没有任何效果。

到目前为止,这是我的代码:

public class Layout extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionbar);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar);

}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // action with ID action_refresh was selected
        case R.id.action_refresh:
            Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
                    .show();
            break;
        // action with ID action_settings was selected
        case R.id.action_settings:
            Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
                    .show();
            break;
        default:
            break;
    }

    return true;
}

}

这是我的R.layout.action:

<?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="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="my Title"
    android:textColor="#ffffff"
    android:id="@+id/mytext"
    android:textSize="18sp"
    android:gravity="center"
    />

事实是,您实际上不应该将ActionBar(或Toolbar,取决于您使用的工具)中的Text居中对齐-这就是为什么这样做不那么简单。

如果您将其对齐,保持当前状态,则应与72dp关键线对齐(这将与常规“材料指南”相匹配)。

您可以在以下位置找到这些规范: https : //www.google.com/design/spec/layout/structure.html#structure-app-bar (但同样,如果您什么也不做,一切都应该可以正常工作)

看到了,您可能已经看到某些应用程序在操作栏(工具栏)上具有居中文本,但是必须自定义布局或使用基本编码,API应当大于5.0才能实现这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM