我有一个带有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"
/>
答案 0 :(得分:0)
问题在于你并不是真的应该将ActionBar(或工具栏中的文本)居中对齐,这取决于你使用的是什么 - 这就是为什么它不简单。
如果你保持它当前的对齐状态,它应该与72dp keyline对齐(这将符合一般材料指南)。
你会在这里找到这些规范:https://www.google.com/design/spec/layout/structure.html#structure-app-bar(但是,如果你什么都不做,一切都应该正常工作)
答案 1 :(得分:0)
请参阅,您可能已经看到某些应用程序在操作栏(工具栏)上设置了居中文本,但布局必须是自定义的,或者使用基本编码API应该> 5.0以实现此类方案。