4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[React Nativeレイアウト覚書] flexとwidth/heightの適用優先度を確認

Last updated at Posted at 2018-11-01

1.普通のflex

<View style={{backgroundColor: 'red', width: 100, height: 100}}>
  <View style={{backgroundColor: 'blue', flex: 1}}/>
</View>

背景redである親要素の内側を背景blueの要素で満たす
スクリーンショット 2018-11-01 14.28.23.png

2.width, heightを加える

<View style={{backgroundColor: 'red', width: 100, height: 100}}>
  <View style={{backgroundColor: 'blue', flex: 1, width: 50, height: 50}}/>
</View>

値を指定したwidthは効くが、heightは効かず、flexが有効となりストレッチする。これは親要素のflexDirection(デフォルトcolumn)が関係している。
=> 次で説明
スクリーンショット 2018-11-01 14.31.00.png

3.flexDirectionを変更する

<View style={{backgroundColor: 'red', width: 100, height: 100, flexDirection: 'row'}}>
  <View style={{backgroundColor: 'blue', flex: 1, width: 50, height: 50}}/>
</View>

2とはwidthとheightの効きが逆転する。
これは子要素のflex=1が親要素のflexDirectionの向きに対してストレッチするため。
スクリーンショット 2018-11-01 14.33.56.png

4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?