1-4-2 Responsive Material Design layouts


title: Responsive Material Design layouts
summary: "How to create responsive Material Design layouts
with Paper and Iron elements."
tags: ['layout']
elements: ['paper-header-panel','paper-toolbar','paper-drawer-panel',
'paper-icon-button','paper-tabs','paper-tab','paper-drawer-panel', 'iron-icons',
'iron-flex-layout']
updated: 2015-07-23


<style>
paper-button {
background: #81C784;
color: white;
}
</style>

Introduction

This guide teaches you how to use Paper and Iron elements to create a
responsive layout.

Installation

Below is a list of commands for installing all of the elements mentioned
in this document. You probably
do not need to install all of these elements. Read the guide and decide
how you want to implement your layout, and then install only the elements
that you need.

bower install --save PolymerElements/paper-header-panel
bower install --save PolymerElements/paper-toolbar
bower install --save PolymerElements/paper-drawer-panel
bower install --save PolymerElements/paper-icon-button
bower install --save PolymerElements/paper-tabs
bower install --save PolymerElements/paper-tab
bower install --save PolymerElements/iron-icons
bower install --save PolymerElements/iron-flex-layout

We'll assume that you can import these elements from /bower_components/.

Creating a header

This section shows you how to:

  • Create a standard layout with paper-header-panel and paper-toolbar,
    which is probably the most common and easiest layout.
  • Use a custom element for a header.
  • Add icons to a header.
  • Set the height of a header.
  • Add tabs to a header.
  • Modify the disply and behavior of a header.

Creating a header with paper-toolbar

The code below uses a paper-header-panel as the container of the
page and a paper-toolbar as a header. When a paper-toolbar is a
child of paper-header-panel, the panel automatically displays
the toolbar as the header. All other
children are placed in the content area.

...
<head>
...
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <!-- paper-header-panel must have an explicit height -->
  <paper-header-panel class="flex">
    <paper-toolbar>
      <div>Header</div>
    </paper-toolbar>
    <div>Content</div>
  </paper-header-panel>
</body>
...

<a href="assets/header-and-toolbar.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

paper-header-panel must have an explicit height. See the list item
on flex below for an explanation of why the code above works.

fullbleed, vertical, layout, and flex are helper classes from
iron-flex-layout.html. We use them in our examples as an easy way
to create a responsive design with Flexbox,
but the paper elements do not depend
on them. Below is a description of each class used in the example above:

  • fullbleed instructs body to occupy the entire viewport.
  • vertical and layout instruct body to stack elements
    vertically (use vertical horizontal to stack horizontally). layout
    must be accompanied by vertical or horizontal. It is meaningless
    on its own.
  • flex instructs paper-panel-header to stretch to the entire
    size of its parent, in this case body (which is set to fill the entire
    viewport, hence achieving a responsive design).

See Flexbox layout with iron-flex-layout for more
on iron-flex-layout.

Using other elements for the header

You can use another element as a header by adding the
paper-header class to the element.

<head>
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <paper-header-panel class="flex">
    <div class="paper-header">
      Header
    </div>
    <div>Content</div>
  </paper-header-panel>
</body>

<a href="assets/custom-header.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

Adding icons

Use paper-icon-button and iron-icons to add icons to your header:

...
<head>
...
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" 
        href="/bower_components/paper-icon-button/paper-icon-button.html">
  <link rel="import" 
        href="/bower_components/iron-icons/iron-icons.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <paper-header-panel class="flex">
    <paper-toolbar>
      <div>Header</div>
      <span class="flex"></span>
      <paper-icon-button icon="search"></paper-button-icon>
    </paper-toolbar>
    <div>Content</div>
  </paper-header-panel>
</body>

<a href="assets/icons.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

paper-icon-button displays the icon and handles the icon's behavior.
iron-icons is a collection of SVG icons which you can use for free
in your project.

How does the search icon display on the right side? The trick
is the span between the div and the paper-icon-button.
The div containing the text Header only takes up as
much space as is needed to display
the text content. Same with the paper-icon-button; it only takes up
as much space as is needed to display the icon. The flex
class forces the span to fill the entire space between the div and
the paper-icon-button.

Setting the height

Use the medium-tall (2x regular height) and tall (3x regular height) style
classes to change the height of your header.

...
<head>
...
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <paper-header-panel class="flex">
    <paper-toolbar class="tall">
      <div>Header</div>
    </paper-toolbar>
    <div>Content</div>
  </paper-header-panel>
</body>
...

<a href="assets/tall-header.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

Adding tabs

Use paper-tabs to add tabs to your header:

...
<head>
...
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" 
        href="/bower_components/paper-icon-button/paper-icon-button.html">
  <link rel="import"
        href="/bower_components/paper-tabs/paper-tabs.html">
  <link rel="import" 
        href="/bower_components/iron-icons/iron-icons.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <paper-header-panel class="flex">
    <paper-toolbar class="medium-tall">
      <paper-icon-button id="navicon"
                         icon="menu"></paper-icon-button>
      <!-- flex class forces span to fill space between icons -->
      <span class="flex">Title</span>
      <!-- icon displays at right because of span class above -->
      <paper-icon-button id="morebutton"
                         icon="more-vert"></paper-icon-button>
      <paper-tabs class="bottom fit" selected="0">
        <paper-tab>ONE</paper-tab>
        <paper-tab>TWO</paper-tab>
      </paper-tabs>
    </paper-toolbar>
    <div>Content</div>
  </paper-header-panel>
</body>
...

<a href="assets/tabs.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

Modifying header display and behavior

Use the mode attribute of paper-header-panel to control how the
header displays and responds to scrolling. The list below describes
the different valid values for mode. See the link below for a
demonstration of all modes.

  • standard: The header appears at a higher level than the content area,
    with a drop shadow. Content scrolls under the header.
  • seamed: The header appears at the same level as the content area,
    with a seam between the two (no drop shadow). Content scrolls under the header.
  • waterfall: The header initially presents as seamed. When content scrolls
    under the header, the header raises up and casts a drop shadow (as in
    standard mode).
  • waterfall-tall: Like waterfall, except that the toolbar starts off
    tall (3x standard height) and condenses to a standard-height
    toolbar as the user scrolls. In this mode, paper-header-panel controls
    the height of the toolbar, so you should not set it yourself (via
    medium-tall or tall).
  • scroll: The header is seamed with the content and scrolls with the content.
  • cover: The content scrolls over the header. This mode is designed to
    be used with narrow content (for example cards).

<a href="/elements/paper-header-panel?view=demo:demo/index.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

Creating a responsive navigation drawer

Use paper-drawer-panel to create a left-side or right-side
navigation menu.

<head>
  <link rel="import"
        href="/bower_components/paper-drawer-panel/paper-drawer-panel.html">
  <link rel="import" 
        href="/bower_components/paper-header-panel/paper-header-panel.html">
  <link rel="import" 
        href="/bower_components/paper-toolbar/paper-toolbar.html">
  <link rel="import" 
        href="/bower_components/paper-icon-button/paper-icon-button.html">
  <link rel="import" 
        href="/bower_components/iron-icons/iron-icons.html">
  <link rel="import" 
        href="/bower_components/iron-flex-layout/iron-flex-layout.html">
...
<body class="fullbleed vertical layout">
  <paper-drawer-panel class="flex">
    <paper-header-panel drawer>
      <paper-toolbar>
        <div>Application</div>
      </paper-toolbar>
      <div> Drawer content... </div>
    </paper-header-panel>
    <paper-header-panel main>
      <paper-toolbar>
        <paper-icon-button icon="menu" paper-drawer-toggle></paper-icon-button>
        <div>Title</div>
      </paper-toolbar>
      <div> Main content... </div>
    </paper-header-panel>
  </paper-drawer-panel>
</body>

<a href="assets/drawer.html" target="_blank">
<paper-button noink>Demonstration</paper-button>
</a>

On narrow screens, the drawer is hidden by default. The user can
touch the button or swipe in order to display the drawer.
On wide screens, the drawer is always open and the button to open
the drawer is hidden.

Use the togglePanel method to hide or reveal the drawer
programmatically. Or, add the paper-drawer-toggle attribute to an
element. This attribute makes the element act as an open / close button and
removes the need to call togglePanel explicitly.

Any children with the drawer attribute set are placed in the navigation area.
Any children with the main attribute are placed in the main panel.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,702評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,615評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,606評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,044評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,826評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,227評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,307評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,447評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,992評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,807評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,001評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,550評論 5 361
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,243評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,667評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,930評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,709評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,996評論 2 374

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,713評論 0 23
  • 你是否見識過這個世界的冷漠,無情以及殘酷? 我們總是習慣性的關注美好的事物,習慣性的歌頌陽光,贊美善良。是因為大多...
    毒舌的言晚閱讀 484評論 0 1
  • 今天是周五的晚上,爸爸回來了。爸爸,回來的時候已經筋疲力盡,但是他帶回了一個好吃的就是烤鴨。我看見了烤鴨,...
    limaolin閱讀 392評論 0 2
  • 護膚經驗小知識: 外線 “光老化”等級:★★★★★ 陽光中的紫外線會穿透玻璃進入室內,所以只要在距離窗戶4m范圍之...
    jcy瑩閱讀 640評論 0 1
  • 在不得不離開的時候,開始全新跟不同,習慣一直想卻未習慣的app,做想卻一直沒做的事,總之,將自己與過去分離,來隱去...
    Sheenagh閱讀 263評論 0 0