2016-04-30 43 views
0

我得到了一个带有3个菜单项目的操作栏,可悲的是这些菜单项在左侧对齐,我会在中心对齐。我一直在环顾四周,尝试了一些东西,但没有任何工作。如何在Android上的动作栏中对齐中心项目

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

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" 
    /> 

回答

0

的事情是,你是不是真的应该居中对齐在ActionBar(或工具栏,取决于你正在使用的)中的文本 - 这就是为什么它不是简单的事情。

如果您将其保持为当前对齐状态,则应该与72dp角线对齐(它将与通用材质指南相匹配)。

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

+0

感谢您的评论。可悲的是,这并没有真正帮助解决我的问题,这可能不是正确的做法,但这是我在Android应用程序中看到的很多东西。也许actionBar不是我的情况下使用的组件,如果是的话,哪一个会是? –

+0

那么使用ActionBar以外的东西将非常困难(有许多功能内置到ActionBar中,您将不得不复制)。也只是因为你看到另一个应用程序这样做,并不意味着它很容易(或者说它应该完成) – Booger

0

看,你可能已经看到了一些有动作栏(工具栏)上居中的文本的应用程序,但是必须定制布局或者使用基本编码来实现这种情况,API应该大于5.0。