# -- 第六课：自定义样式

我们快要完成这个应用了，做完一点点自定义样式之后，就算正式完成了。在定义我们的样式之前我们将要给模板文件添加一些类。\
\&#xNAN;**> 修改src/pages/home/home.ts 为如下：**

```markup
<ion-header>
    <ion-navbar color="secondary">
        <ion-title>
        <ion-searchbar color="primary" placeholder="enter subreddit name..."   [(ngModel)]="subredditValue" [formControl]="subredditControl"  value=""></ion-searchbar>
        </ion-title>
        <ion-buttons end>
        <button ion-button icon-only (click)="openSettings()"><ion-icon   name="settings"></ion-icon></button>
        </ion-buttons>
    </ion-navbar>
</ion-header>
<ion-content>
<ion-list>
    <div *ngFor="let post of redditService.posts">
    <ion-item (click)="playVideo($event, post)" no-lines  style="background-color: #000;">
    <img src="assets/images/loader.gif" *ngIf="post.showLoader" class="video-loader" />
    <video loop [src]="post.data.url" [poster]="post.data.snapshot">
    </video>
    </ion-item>
    <ion-list-header (click)="showComments(post)" style="text-align: left;">
    {{post.data.title}}
    </ion-list-header>
    </div>
    <ion-item *ngIf="redditService.loading" no-lines style="text-align:center;">
    <img src="assets/images/loader.gif" style="width: 50px" />
    </ion-item>
    </ion-list>
    <button ion-button color="light" full (click)="loadMore()">Load More...</button>
</ion-content>
```

如果你复习过基础部分的话，你应该知道有一些定义样式的方法，如果你跳过了基础部分的定制主题的话，强烈建议你回去读一遍。 **> 修改src/pages/home/home.scss 为如下：**

```css
page-home {

    ion-item {
        background-color: #000;
        position: relative;
    }

    ion-item .video-loader {
        position: absolute;
        top: 10px;
        right: 10px;
        width: 25px;
        height: 25px;
    }

    ion-list-header {
        background-color: #fff;
    }

    ion-label {
        margin: 0px !important;
    }

    ion-list {
        margin: 0px !important;
    }

    ion-item-content, .item-inner, .item {
        margin: 0 !important;
        padding: 0 !important;
        text-align: center;
    }

    video {
        max-width: 100%;
        height: auto;
        background-color: #000;
    }
}
```

这些都是很基本的样式，大部分我们都只是改变一下颜色，移除间隙和边距。这里最重要的样式是*ion-item-content*和*video*样式用于缩放视频以适配离别。理想情况下我们希望视频从宽度上填满整个项，但是本案例中GIF不会都有肖像，所以我们让GIF居中显示，然后添加黑色背景，这样其实也蛮好看的。\
现在我们将针对iOS做一些特别的样式。我们顶部的搜索条看起来有点小，因为我们把他加入到部分，所以我们得把他变大点。搜索条在Android的显示很好，所以我们不想要样式在Android上产生作用。\
\&#xNAN;**> 添加如下样式到 src/app/app.scss：**

```css
.ios {
    ion-title {
        padding: 0px 35px 1px 0px;
    }
}
```

当在iOS上运行的时候，Ionic将自动添加一个ios类&#x5230;*<\_body>\_*&#x8FD9;样我们可以通过以上方法对iOS进行定制。同理，也可以通过*md*对Android进行定制，*wp*对Windows进行定制。接下来，我们要给body标签添加一个背景色，这个我们将会在同一&#x7684;*.scss*文件中添加。\
同时我们也会对整个应用进行样式定制。\
\&#xNAN;**> 将以下样式添加到 src/app/app.scss：**

```css
body {
    background-color: #e74c3c;
}
```

终于，我们来到了定义应用共享变量的地方了，**variables.scss**文件。\
\&#xNAN;**> 修改 src/theme/variables.scss到如下样式：**

```css
$colors: (
    primary: #ecf0f1,
    secondary: #e74c3c,
    danger: #f53d3d,
    light: #e74c3c,
    dark: #222222
);

$searchbar-ios-background-color: #e74c3c;
$searchbar-ios-input-background-color: #c94234;

$button-ios-border-radius: 0px;
```

此处我们进行了一些颜色变更，对搜索条进行了一些修改，设置了iOS的按钮边缘半径让我们的‘Load More’从矩形变成了圆角矩形。 ![预览](/files/-LBty3VMNR3yYFsajtkX)

好看多了！如果你查看Android版本的话（在Chrome Dev Tools中设置Emulator为Samsung或者其他Android设备，或者使用*ionic server -l*命令），效果是这样的：\
![预览](/files/-LBty3YYQ0NhXOAa7_32)

（请忽略右边的白色条，他不会出现在真实设备上的）\
他看起来明显不同了，但是看起来还是很不错。Ionic最好的事情之一是他自动根据运行平台进行调整适配。通常，越少跟平台纠缠越好。如果你主要是用iOS设备的话，或者主用Android设备，其他平台的样式看起来可能会有点怪异，但是此平台用户而言可能不会那么怪异。如果有很好的理由，否则不应该去改动平台特定样式（当然可以实现的）来让他看起来跟另一平台相同。

## 总结

跟其他课程相比本节课相当简短，但是同时他也给你展示了付出一点点的努力为应用制作一个不错的自定义主题是多么的简单。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://things.gitbook.io/ionic-2/di-liu-ke-zi-ding-yi-yang-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
