如何在 Android Lollipop 上声明扩展高度的工具栏/操作栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26491689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I declare an extended height Toolbar/Action Bar on Android Lollipop?
提问by Chris Banes
回答by Chris Banes
You need to use the new Toolbarwidget to achieve this. Toolbar has special handling for it's minimum height to declare the amount of space which is used for buttons (and actions).
您需要使用新的工具栏小部件来实现这一点。工具栏对其最小高度进行了特殊处理,以声明用于按钮(和操作)的空间量。
In the example below, we're setting the height to be 128dp (which is 56dp + 72dp as defined in the spec), but keeping the android:minHeight
as the standard actionBarSize
(which is usually 56dp). This means that the buttons and actions are constrained to be positioned vertically in the top 56dp. We can then use android:gravity
to position the title at the bottom.
在下面的示例中,我们将高度设置为 128dp(即规范中定义的 56dp + 72dp),但保持android:minHeight
标准actionBarSize
(通常为 56dp)。这意味着按钮和操作被限制为垂直放置在顶部 56dp 中。然后我们可以使用android:gravity
将标题定位在底部。
<Toolbar
android:id="@+id/toolbar"
android:layout_height="128dp"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom" />
If you're using AppCompat, then change the declaration to use android.support.v7.widget.Toolbar
instead and use it's attributes.
如果您使用的是 AppCompat,则更改要使用的声明android.support.v7.widget.Toolbar
并使用它的属性。
回答by Mathias Seguy Android2ee
Thanks for your question, its answer, and moreover for the implementation of the toolbar in the native and the supportlibrary :)
感谢您的问题及其回答,此外还感谢您在本机和支持库中实现工具栏:)
And we can play more. We can, at runtime, play with the Height and the MinimalHeight.
我们可以玩更多。我们可以在运行时使用 Height 和 MinimalHeight。
The height is the ToolBar height, it's simple, every body understand, and the gravity acts according to that height.
高度就是ToolBar的高度,很简单,大家都懂,重力就是按照那个高度来作用的。
The minimalHeight is more tricky and should not be at minimum 56dp. This minHeight is used to place the line of your menuItem. This line is at the midlle of your minHeight.
minimumHeight 更棘手,不应至少为 56dp。此 minHeight 用于放置 menuItem 的行。这条线在你的 minHeight 的中间。
So you can add this code to your activity to see by yourself the difference. :)
因此,您可以将此代码添加到您的活动中以自行查看差异。:)
Runnable toolBarAnimator=new Runnable() {
@Override
public void run() {
if(postICS){
// toolbar.setTranslationX(iteration);
// toolbar.setElevation(iteration);
toolbar.setMinimumHeight(iteration * 5);
toolbar.getLayoutParams().height++;
}
uiThreadHanlder.postDelayed(this,16);
iteration++;
if(iteration>150)iteration=0;
}
};
Handler uiThreadHanlder=new Handler();
int iteration=0;
@Override
protected void onResume() {
super.onResume();
//launch the animation
uiThreadHanlder.postDelayed(toolBarAnimator, 1000);
}
@Override
protected void onPause() {
super.onPause();
//stop the animation
uiThreadHanlder.removeCallbacks(toolBarAnimator);
}
Where toolbar is:
工具栏在哪里:
toolbar = (Toolbar) findViewById(R.id.toolbar);
工具栏 = (工具栏) findViewById(R.id.toolbar);
When doing this you obtain:
这样做时,您将获得:
but if you leave the animation continue you obtain:
但是如果你离开动画继续,你会得到:
This is why, setting your toolbar android:layout_height to wrap_content is a good option in most of the case, because the Toolbar will adapt its height according to its contents (and you can change the content at runtime :)
这就是为什么在大多数情况下将工具栏 android:layout_height 设置为 wrap_content 是一个不错的选择,因为工具栏会根据其内容调整其高度(并且您可以在运行时更改内容:)
And this is also how you change your toolbar size at runtime.
这也是您在运行时更改工具栏大小的方式。
Thanks Chris Banes for the amazing work you did on the actionbar.
感谢 Chris Banes 在操作栏上所做的出色工作。