所以我一直在尝试在我的Activity中实现android.support.v7.widget.Toolbar
,并使其看起来与之前支持的拆分ActionBar类似。
以下是我工具栏的XML:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_btm"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/toolbar_bkgnd"
android:layout_alignParentBottom="true"
app:theme="@style/ToolBarTheme" />
这是我正在使用的工具栏的样式:
<style name="ToolBarTheme" parent="Theme.AppCompat">
<item name="actionButtonStyle">@style/ActionButtonStyle</item>
<item name="android:actionButtonStyle">@style/ActionButtonStyle</item>
<item name="android:textColor">@android:color/white</item>
</style>
工具栏菜单按钮的样式,我的初步计划是根据屏幕大小计算minWidth
,然后为每个菜单按钮设置它。
<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
<item name="android:minWidth">56dip</item>
<item name="android:paddingLeft">0dip</item>
<item name="android:paddingRight">0dip</item>
</style>
最后,这就是我在活动中所说的。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_btm);
toolbarBtm.inflateMenu(R.id.menu);
问题是底部Toolbar
中的菜单项是这样对齐的:
但是我希望它们像这样均匀分布:
答案 0 :(得分:10)
Here's对我有用*:
EnhancedMenuInflater.java
import android.support.v4.internal.view.SupportMenuItem;
import android.support.v7.internal.view.menu.MenuItemImpl;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import here.is.your.R;
public class EnhancedMenuInflater {
public static void inflate(MenuInflater inflater, Menu menu, boolean forceVisible) {
inflater.inflate(R.menu.menu, menu);
if (!forceVisible) {
return;
}
int size = menu.size();
for (int i = 0; i < size; i++) {
MenuItem item = menu.getItem(i);
// check if app:showAsAction = "ifRoom"
if (((MenuItemImpl) item).requestsActionButton()) {
item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
}
}
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (toolbar == null) {
EnhancedMenuInflater.inflate(getMenuInflater(), menu, false);
}
return super.onCreateOptionsMenu(menu);
}
// somewhere after views have been set.
if (toolbar != null) {
EnhancedMenuInflater.inflate(getMenuInflater(), toolbar.getMenu(), true);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
});
}
SplitToolbar.java
import android.content.Context;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class SplitToolbar extends Toolbar {
public SplitToolbar(Context context) {
super(context);
}
public SplitToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
if (child instanceof ActionMenuView) {
params.width = LayoutParams.MATCH_PARENT;
}
super.addView(child, params);
}
}
Layout.xml
<here.is.my.SplitToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
当我说工作时,我的意思是它在我的菜单,文字和图像中都集中了一切。如果您只使用菜单图标,那么它看起来会很棒。我还在寻找一种方法让它们居中,让文字就在图标旁边。
答案 1 :(得分:10)
<强>更新强>
谷歌现在拥有非常相似的功能,使用新的Widget调用BottomNavigationView
夸大菜单膨胀的正常方式- 原始答案 -
伙计们花了我一些时间来弄清楚,在这里你会进行一些繁重的操作,但它确实有效。
我在Toolbar
上使用它来显示屏幕底部,就像旧的SplitActionBar
...
在工具栏上显示均匀分布的MenuItem
我不建议使用超过5或6个项目,它可能会有点拥挤...
/**
* This method will take however many items you have in your
* menu/menu_main.xml and distribute them across your devices screen
* evenly using a Toolbar. Enjoy!!
*/
public void setupEvenlyDistributedToolbar(){
// Use Display metrics to get Screen Dimensions
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
// Toolbar
mToolbar = (Toolbar) findViewById(R.id.navigationToolbar);
// Inflate your menu
mToolbar.inflateMenu(R.menu.menu_bottom);
// Add 10 spacing on either side of the toolbar
mToolbar.setContentInsetsAbsolute(10, 10);
// Get the ChildCount of your Toolbar, this should only be 1
int childCount = mToolbar.getChildCount();
// Get the Screen Width in pixels
int screenWidth = metrics.widthPixels;
// Create the Toolbar Params based on the screenWidth
Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);
// Loop through the child Items
for(int i = 0; i < childCount; i++){
// Get the item at the current index
View childView = mToolbar.getChildAt(i);
// If its a ViewGroup
if(childView instanceof ViewGroup){
// Set its layout params
childView.setLayoutParams(toolbarParams);
// Get the child count of this view group, and compute the item widths based on this count & screen size
int innerChildCount = ((ViewGroup) childView).getChildCount();
int itemWidth = (screenWidth / innerChildCount);
// Create layout params for the ActionMenuView
ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, LayoutParams.WRAP_CONTENT);
// Loop through the children
for(int j = 0; j < innerChildCount; j++){
View grandChild = ((ViewGroup) childView).getChildAt(j);
if(grandChild instanceof ActionMenuItemView){
// set the layout parameters on each View
grandChild.setLayoutParams(params);
}
}
}
}
}
答案 2 :(得分:3)
检查一下。
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:abc="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/action1"
android:background="@color/red_700"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/action2"
android:background="@color/red_200"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/action3"
android:background="@color/red_100"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
将ImageView
替换为您想要的任何内容。
答案 3 :(得分:1)
这是我发布的另一个类似问题的解决方案,因为在我的底部工具栏上我想要等间距的按钮: android add two toolbars in the same activity
答案 4 :(得分:1)
此解决方案充分利用了上述每种解决方案,
感谢inner_class7,Kuffs&amp; MrEngineer13。
此解决方案均匀分布菜单项并显示文本。
public class EvenlyDistributedToolbar扩展android.support.v7.widget.Toolbar {
private View actionMenuView;
public EvenlyDistributedToolbar(Context context) {
super(context);
setContentInsetsAbsolute(0, 0);
}
public EvenlyDistributedToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
setContentInsetsAbsolute(0, 0);
}
public EvenlyDistributedToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setContentInsetsAbsolute(0, 0);
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
if (child instanceof ActionMenuView) {
actionMenuView = child ;
params.width = LayoutParams.MATCH_PARENT;
((ViewGroup)actionMenuView).setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
@Override
public void onChildViewRemoved(View parent, View child) {
}
@Override
public void onChildViewAdded(View parent, View child) {
if (child instanceof ActionMenuItemView) {
//Show the menu item text as well as the the icon
ActionMenuItemView actionMenuItemView = (ActionMenuItemView) child;
// set the layout parameters on each View
actionMenuItemView.setExpandedFormat(true);
Drawable[] arr = actionMenuItemView.getCompoundDrawables();
if (arr != null && arr.length == 4 && arr[0] != null) {
actionMenuItemView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
}
else if (arr != null && arr.length == 4 && arr[2] != null) {
actionMenuItemView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
}
actionMenuItemView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
actionMenuItemView.setOnLongClickListener(null);
}
}
});
}
super.addView(child, params);
}
/**
* Show All items, call after the menu inflated
*/
public void showAll() {
Menu menu = getMenu();
int size = menu.size();
for (int i = 0; i < size; i++) {
MenuItem item = menu.getItem(i);
// check if app:showAsAction = "ifRoom"
if (((MenuItemImpl) item).requestsActionButton()) {
item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
}
}
<com.util.EvenlyDistributedToolbar
android:layout_width="match_parent"
android:layout_height="wrap_content" />
答案 5 :(得分:0)
如果以编程方式创建菜单而不是通过资源扩充,则可以执行以下操作:
使用另一个答案中提到的SplitToolbar。正常使用FindViewById获取对工具栏的引用。如果布局中不存在工具栏,则菜单将作为普通的非拆分版本。
import android.content.Context;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class SplitToolbar extends Toolbar {
public SplitToolbar(Context context) {
super(context);
}
public SplitToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
if (child instanceof ActionMenuView) {
params.width = LayoutParams.MATCH_PARENT;
}
super.addView(child, params);
}
}
然后在菜单创建代码中执行以下操作。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (toolbar != null) {
toolbar.setContentInsetsAbsolute(0,0);
menu = toolbar.getMenu();
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Call back to the original menu code to handle menu clicks
return onOptionsItemSelected(menuItem);
}
});
}
// Now build your menu as normal
menu.clear();
MenuItem b = menu.add(0, WHATEVER, 0, R.string.WHATEVER);
b.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
b.setIcon(R.drawable.ic_menu_encrypt);
// End of normal menu code
// Now set the button options.
if (toolbar != null) {
int size = menu.size();
for (int i = 0; i < size; i++) {
MenuItem item = menu.getItem(i);
// check if app:showAsAction = "ifRoom"
if (((MenuItemImpl) item).requestsActionButton()) {
item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
}
Return true;
}
答案 6 :(得分:0)
我的建议是遵循design guidelines。如果您使用的是toolbar,请将menu项保留在设计目的的位置。
但是,如果您想要相等的间距,请考虑使用Tabs
This answer讲述了如何设置底部导航栏。
答案 7 :(得分:0)
由{em> kandroidj 的public void setupEvenlyDistributedToolbar(){}
提出的解决方案非常有效。但是,为了使解决方案更加完善,您需要自定义OnclickListener
:
附上我的实现
private void setupOnClickListener4Toolbar(Toolbar toolbar) {
Menu bottomMenu = toolbar.getMenu();
for (int i = 0; i < bottomMenu.size(); i++) {
bottomMenu.getItem(i).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
});
}
}