如您所见,菜单的3个点或搜索按钮位于工具栏的前三分之一处。我该如何居中呢? 如果它相关 - 我的一些XML:
tool_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:elevation="4dp" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="@drawable/app_icon" />
</android.support.v7.widget.Toolbar>
activity_main.xml中:
<include
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
layout="@layout/tool_bar" >
</include>
<other views>
searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/hint_search"
android:icon="@drawable/magnifying_glass"
android:label="@string/app_name"
android:searchSuggestAuthority="com.app.recommendations.content_provider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData=""
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
main_menu.xml:
...
<item
android:id="@+id/search"
android:icon="@drawable/magnifying_glass"
android:title="@string/hint_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
...
答案 0 :(得分:0)
更改too_bar.xml的代码
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="@drawable/app_icon" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
答案 1 :(得分:0)
您可以使用ActionMenuView创建自定义工具栏。
在您的toolbar.xml中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<android.support.v7.widget.ActionMenuView
android:id="@+id/action_menu"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>
在您的MainActivity中,
public class MainActivity extends AppCompatActivity {
ActionMenuView actionMenuView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionMenuView = (ActionMenuView) toolbar.findViewById(R.id.action_menu);
actionMenuView.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, actionMenuView.getMenu());
return true;
}
}
将ActionViewMenu的重力设置为“居中”。我希望它会对你有所帮助。
答案 2 :(得分:0)
我没有使用android:layout_weight="2"
和android:layout_height="0dp"
,而是将高度设置为wrap_content
,这使得工具栏精简并且居中!