Android的材料,扩展工具栏

Android的材料,扩展工具栏

问题描述:

我测试材料的设计和我使用扩展工具栏上开发的应用程序。我的应用程序是非常简单的:主要活动来延长 ActionBarActivity 和我的布局是这样的:

I'm testing material design and i'm developing an app using extended toolbar. My app is very simple: The main activity extends ActionBarActivity and my layout looks like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WeatherActivity"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:layout_height="128dp"
        popupTheme="@style/ActionBarPopupThemeOverlay"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="@string/location_placeholder"
        android:textAlignment="viewStart"
        android:layout_gravity="start"
        />
</LinearLayout>

现在我想显示为标题的当前位置。的第一件事,我注意到在我的Andr​​oid模拟器(API 18)的标题似乎并不尊重有关左边距底部边缘材料的指导方针,它出现在左侧工具栏里面输入垂直。所以,我应该使用的工具栏标题(toolbar.setTitle)或别的东西? 其次,如果我想创建更复杂的东西像标题和简短说明(如布局结构显示在材料准则)应该是什么我的布局? THX的支持!

Now i'd like to show as title the current location. The first thing i'm noticing is in my Android Emulator (API 18) the title doesn't seem to respect material guidelines about left margin bottom margin, it appears on the left side and entered vertically inside the Toolbar. So Should i use tool bar title (toolbar.setTitle) or something else? Second if i want to create something more complex like Title and a short description (as shown in the material guidelines in the layout structure) what should be my layout? Thx for your support!

好您的活动来延长 ActionBarActivity ,所以你也必须确保的主题为这项活动是一个儿童无论是 Theme.AppCompat.NoActionBar Theme.AppCompat.Light.NoActionBar 。如果不使用 Theme.AppCompat 变体,那么你可以将下列行交替添加到您的主题:

Ok your activity extends ActionBarActivity so you also have to make sure that the theme for this activity is a child of either Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar. If you are not using the Theme.AppCompat variants then you can alternatively add the following lines to your theme:

<item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item> 

然后,所有你需要做的是工具栏添加到您的布局(它看起来像你已经有了):

Then all you need to do is add the Toolbar to your layout (which it looks like you already have):

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:layout_height="128dp"
    popupTheme="@style/ActionBarPopupThemeOverlay"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

然后在你的活动设置工具栏是通过你的动作条 setSupportActionBar

    Toolbar mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(mToolbar);

这是最后一个部分是所有魔术发生,因为你说的Andr​​oid,借此工具栏小工具,并使用它的完全的你将如何使用SupportActionBar。这意味着,如果要设置标题/字幕所有你需要做的就是调用:

That last part is where all the magic happens because you're saying "Android, take this Toolbar widget and use it exactly how you would use the SupportActionBar". This means that if you want to set the title/subtitle all you need to do is call:

    getSupportActionBar().setTitle("Toolbar Title");
    getSupportActionBar().setSubtitle("Toolbar Subtitle");

这也意味着你可以使用相同的回调创建工具栏上的菜单。

This also means that you can use the same callbacks to create a menu on the Toolbar.

要直接回答你的问题:

这样,我使用的工具栏标题(toolbar.setTitle)或其他什么东西?

So Should i use tool bar title (toolbar.setTitle) or something else?

您实际上可以使用, toolbar.setTitle() getSupportActionBar()的setTitle()

You can actually use either, toolbar.setTitle() or getSupportActionBar().setTitle()

二,如果我想创造的东西更像是标题和简短说明(如图中的布局结构材料准则)应该是什么我的布局?复杂

Second if i want to create something more complex like Title and a short description (as shown in the material guidelines in the layout structure) what should be my layout?

工具栏支持标题和字幕,所以你应该设置。我想结帐的文档只是为了看看所有的工具栏支持。作为一般规则,如果动作条能做到这一点,工具栏即可。如果你有超出支持工具栏的疯狂需求,然后只记得工具栏只是一个花哨的ViewGroup这样你就可以添加小工具/视图一样容易,就好像它是一个LinearLayout中。

Toolbar supports Titles and Subtitles so you should be set. I would checkout the documentation just to see what all the Toolbar supports. As a general rule, if the Actionbar could do it, the Toolbar can. If you have crazy requirements beyond what is supported by the Toolbar then just remember that the Toolbar is just a fancy ViewGroup so you can add widgets/views just as easily as if it were a LinearLayout.