簡體   English   中英

React-native使用Flexbox定義組件高度

[英]React-native define component height with flexbox

我試圖定義一個具有相同高度的元素列表,但結果是該組件占用了所有可用空間。

這是代碼:

    return (
      <View style={{flex: 1, flexDirection: 'row', justifyContent: "center"}} key={location.id}>
        <Text style={{width: "80%", height: 20}}>{location.name}</Text>
        <Button
          style={{width: "20%", height: 20}}
          title="x"
          onPress={() => this.removeLocationFromDevice(location)}
        />
      </View>
    )

結果如下:

這是結果

如何確保按鈕和文本元素的高度剛好足以顯示元素,僅此而已?

您可以將初始視圖設置為flex 1,然后將按鈕+文本包裝在另一個視圖中

return (
  <View style={{flex: 1}} key='key'>
  <View style={{ flexDirection: 'row', justifyContent: "center"}}>
  <Text style={{width: "80%", height: 20}}>text</Text>
  <Button
    style={{width: "20%", height: 20}}
    title="x"
  />
  </View>
</View>
);

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM