如何将膨胀的自定义视图与工具栏中的其余菜单项对齐
How do I align an inflated custom view with the rest of the menu items in Toolbar
我真的不知道怎么放这个,但我只想要一个工具栏(操作栏),就像下面的 WhatsApp 样式一样。
与此同时,我使用了 Android 工具栏并扩展了一个自定义视图(包含购物车按钮)并使用 setCustomView() 方法添加了它。我尝试调整 TextSize 属性 TextView 但即使是最合理的尺寸也不对齐 TextView 与 工具栏 菜单项的其余部分。关于如何执行此操作的任何帮助?或者有不同的方法吗?
如果我对你的理解是正确的,我建议采用不同的方法,你可以将工具栏设置为 SupportActionBar 并添加你的自定义图标,就像你在普通的 actionBar 中所做的那样
- 将工具栏设置为 SupportActionBar
例如。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- 像对普通 actionBar 一样创建菜单文件
例如。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action1"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="@drawable/custom_icon1"
app:showAsAction="always" />
<item
android:id="@+id/action2"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="@drawable/custom_icon2"
app:showAsAction="ifRoom" />
</menu>
- 最后使用 onCreateOptionsMenu 和 onOptionsItemSelected 完成设置。
例如
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
.....
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_custom_view:
.....Your code.....
break;
case android.R.id.action_custom_view2:
.....Your code.....
break;
}
return false;
}
我真的不知道怎么放这个,但我只想要一个工具栏(操作栏),就像下面的 WhatsApp 样式一样。
与此同时,我使用了 Android 工具栏并扩展了一个自定义视图(包含购物车按钮)并使用 setCustomView() 方法添加了它。我尝试调整 TextSize 属性 TextView 但即使是最合理的尺寸也不对齐 TextView 与 工具栏 菜单项的其余部分。关于如何执行此操作的任何帮助?或者有不同的方法吗?
如果我对你的理解是正确的,我建议采用不同的方法,你可以将工具栏设置为 SupportActionBar 并添加你的自定义图标,就像你在普通的 actionBar 中所做的那样
- 将工具栏设置为 SupportActionBar
例如。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- 像对普通 actionBar 一样创建菜单文件
例如。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action1"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="@drawable/custom_icon1"
app:showAsAction="always" />
<item
android:id="@+id/action2"
android:orderInCategory="100"
android:title="CUSTOM TITTLE"
android:icon="@drawable/custom_icon2"
app:showAsAction="ifRoom" />
</menu>
- 最后使用 onCreateOptionsMenu 和 onOptionsItemSelected 完成设置。
例如
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
.....
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_custom_view:
.....Your code.....
break;
case android.R.id.action_custom_view2:
.....Your code.....
break;
}
return false;
}