2017-12-31 43 views
1

TextサブセットまたはTextコンポーネントサブビューのいずれかを含むカスタマイズされた親Viewを書きたいと思います。 Textビューの高さに基づいて、親をViewの高さに設定する方法はありますか?サブビューの高さに基づいて親ビューの高さを設定する方法

class ParentView extends Component { 
    constructor(props) { 
    super(props); 
    } 

    render() { 
    const { 
     titleText, 
     bodyText, 
    } = this.props; 

    //if titleText is passed to props, measure it's height; 
    //if bodyText is passed to props, measure it's height; 
    // set contentContainer height = titleText + bodyText + someMargin 
    return (
     <View style={styles.contentContainer}>   
     {titleText && <Text style={styles.title}> 
      {titleText} 
     </Text>} 
     {bodyText && <Text style={styles.body}> 
      {bodyText} 
     </Text>} 
     </View> 
    ); 
    } 
} 
+0

なぜ親の高さを設定する必要があるのですか?親が高さ、最小/最大、およびフレックススタイルを単純に省略して、親が子をラップするようにすることができます。 – vovkasm

+0

あなたは答えを試しましたか? – Akis

答えて

0

あなたは、それぞれタイトルテキスト本文テキストを返すか、いずれかが表示され、その機能を使用して、ビュー全体を置き換えるために持っていない場合は、高さゼロでビューを返すために別のヘルパー関数を作成することができます。だから、このようなあなたのコードスニペットを置き換えることができます:

class ParentView extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
    isTitle : props.titleText, 
    isBody: props.bodyText 
    } 
} 

    render() { 

    //if titleText is passed to props, measure it's height; 
    //if bodyText is passed to props, measure it's height; 
    // set contentContainer height = titleText + bodyText + someMargin 
    return (
     {/* include this helper function to return Title Text */}   
    {this.returnText()} 

    ); 
    } 
} 
    returnText() { 
     if(this.state.isTitle && this.state.isBody) { 
      return(
       <View style={styles.contentContainer}>   
        <Text style={styles.title}> 
         {titleText} 
        </Text> 
        <Text style={styles.body}> 
         {bodyText} 
        </Text> 
       </View> 
      ) 
     } 
     else if(this.state.isTitle && !this.state.isBody) { 
      return(
       <View style={styles.contentContainer}>   
        <Text style={styles.title}> 
         {titleText} 
        </Text> 
        <View style = {{height: 0}}/> 
       </View> 
       ) 
     } 
     else if(!this.state.isTitle && this.state.isBody) { 
      return(
       <View style={styles.contentContainer}> 
        <View style = {{height: 0}}/> 
        <Text style={styles.body}> 
         {bodyText} 
        </Text> 
       </View> 
       ) 
     } 
     else { 
      return(
       <View style={styles.contentContainer}> 
        <View style = {{height: 0}}/> 
       </View> 
       ) 
      } 
     } 
0

あなたはそれが子供の高さに応じて拡大します。この方法であなたのViewコンテナ

const styles = StyleSheet.create({ 
    contentContainer: { 
    flex: 0 
    } 
}); 

flex:0を設定することができます。

いくつかの情報についてflexプロパティ:Flexは正の数である場合には

、それは部品が柔軟になり、それ は、そのフレックス値に比例した大きさになります。したがって、フレックスが に設定されているコンポーネントは、フレックスが1に設定されているコンポーネントの2倍のスペースをとります。

フレックスが0の場合、コンポーネントは幅と高さに応じてサイズが変更され、柔軟性に欠けます。

flexが-1の場合、コンポーネントの幅は通常、幅に合わせて の高さになります。ただし、十分なスペースがない場合、コンポーネントは のminWidthおよびminHeightに縮小します。

関連する問題