LoginSignup
129
91

More than 5 years have passed since last update.

React Nativeのデザイン - Stylesheet/Flexbox (part1)

Posted at

React Nativeのアプリはスタイルシートで各コンポーネントの配置、色、フォント等のデザインを決めていきます。Webプログラマなら体に染みついているpadding等を使うので、すぐに慣れると思います。 公式ドキュメントから一覧を見ることができますが、border系は下記のようになります。注意点として、ハイフン(-)で単語を区切るんではなくCamelCaseになっていることです。

borderColor string
borderTopColor string
borderRightColor string
borderBottomColor string
borderLeftColor string
borderRadius number
borderTopLeftRadius number
borderTopRightRadius number
borderBottomLeftRadius number
borderBottomRightRadius number
borderStyle enum('solid', 'dotted', 'dashed')
borderWidth number
borderTopWidth number
borderRightWidth number
borderBottomWidth number
borderLeftWidth number

このような馴染みのものなら直感的に記述していけるのですが、あまり馴染みのないFlexboxの記法を用いて配置を行う必要があるので、Flexboxを取り扱います。

Flexbox

下記のようなコンポーネントを前提に話を進めていきます。

  render: function() {
    return (
      <View style={styles.container}>
        <View style={styles.item1}>
          <Text> Flex Item1</Text>
        </View>
        <View style={styles.item2}>
          <Text> Flex Item2</Text>
        </View>
      </View>
    );
  }

flexプロパティ

おそらく一番最初にハマるのがこのプロパティでしょう。振る舞いがfacebookにより定義されているようです

var styles = StyleSheet.create({
  container: {
    backgroundColor: 'blue',
  },
  item1: {
    backgroundColor: 'gray',
    padding: 20
  },
  item2: {
    backgroundColor: 'gray',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.26.04.png

上記のようなスタイルシートにflex:1をContainerに追加するとそのコンポーネントが全体に広がります。青のコンポーンネントが全体になっているのがわかると思います。

var styles = StyleSheet.create({
  container: {
    flex: 1, // 追加
    backgroundColor: 'blue',
  },
  item1: {
    backgroundColor: 'gray',
    padding: 20
  },
  item2: {
    backgroundColor: 'gray',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.26.25.png

また同一コンポーネント内でflexを下記のように指定すると、

var styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'blue',
  },
  item1: {
    flex: 1, // 追加
    backgroundColor: 'gray',
    padding: 20
  },
  item2: {
    flex: 2, // 追加
    backgroundColor: 'orange',
    padding: 20
  }
});

item1の高さ: item2の高さ = 1 : 2の割合かつ全体に広がって表示されます。

スクリーンショット 2015-12-05 13.30.43.png

flexDirectionプロパティ

container側の要素でこ要素の配置の縦横を決めます。

columnは縦並び(デフォルト)

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column', // 追加。デフォルトはcolumnなので変化なし。
    backgroundColor: 'blue',
  },
  item1: {
    flex: 1,
    backgroundColor: 'gray',
    padding: 20
  },
  item2: {
    flex: 2,
    backgroundColor: 'orange',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.30.43.png

rowで横並び

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row', // 追加
    backgroundColor: 'blue',
  },
  item1: {
    flex: 1,
    backgroundColor: 'gray',
    padding: 20
  },
  item2: {
    flex: 2,
    backgroundColor: 'orange',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.38.33.png

flexWrapプロパティ

Container側(親側)の要素です。コンポーネントが入りきらなかった場合の回り込みの設定になります。

nowrap(デフォルト)

var styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    backgroundColor: 'blue',
    flexWrap: 'nowrap', // デフォルト。回り込みなし。
  },
  item1: {
    backgroundColor: 'gray',
    padding: 20,
    width: 300,
  },
  item2: {
    backgroundColor: 'orange',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.47.12.png

wrapは回り込ませる。

var styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    backgroundColor: 'blue',
    flexWrap: 'wrap',
  },
  item1: {
    backgroundColor: 'gray',
    padding: 20,
    width: 300,
  },
  item2: {
    backgroundColor: 'orange',
    padding: 20
  }
});

スクリーンショット 2015-12-05 13.45.30.png

少し長くなってきたので、明日part2で残りのFlexboxの説明をします。

129
91
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
129
91