我得到了一个带有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"
/>
感谢您的评论。可悲的是,这并没有真正帮助解决我的问题,这可能不是正确的做法,但这是我在Android应用程序中看到的很多东西。也许actionBar不是我的情况下使用的组件,如果是的话,哪一个会是? –
那么使用ActionBar以外的东西将非常困难(有许多功能内置到ActionBar中,您将不得不复制)。也只是因为你看到另一个应用程序这样做,并不意味着它很容易(或者说它应该完成) – Booger