javascript - React Native:视图不覆盖父高度
问题描述
问题
有 2 个选项卡水平堆叠,在第一个选项卡中,当两个选项卡中的内容不同时,绿色视图无法填充父级的高度。
世博会重现
https://snack.expo.dev/@vinay-sharma/92706e
代码
import React from "react";
import { Text, View } from "react-native";
const Tab = ({ textNodes }) => {
return (
<View style={{ flex: 1, backgroundColor: "red" }}>
<View style={{ flexDirection: "row" }}>
<View style={{ backgroundColor: "blue", width: 10 }}></View>
{/* the green view isn’t able to fill parent's height*/}
<View style={{ backgroundColor: "green", flex: 1 }}>
{textNodes.map((node) => (
<Text>{node}</Text>
))}
</View>
</View>
<View style={{ height: 10, backgroundColor: "black" }} />
</View>
);
};
export default function App() {
return (
<View style={{ flexDirection: "row", marginTop: 25 }}>
<Tab textNodes={[1, 2, 3]} />
<Tab textNodes={[1, 2, 3, 4]} />
</View>
);
}
解决方案
Just add style
property height: '100%'
in the row
view in your Tab()
function
The first element is not taking 100% of the height because it has less Childs (default behaviour of a View
, doc)
import React from 'react';
import { Text, View } from 'react-native';
const Tab = ({ textNodes }) => {
return (
<View style={{ flex: 1, backgroundColor: 'red' }}>
<View style={{ flexDirection: 'row', height: '100%' }}> //here add height 100%
<View style={{ backgroundColor: 'blue', width: 10 }}></View>
{/* the green view isn’t able to fill parent's height*/}
<View style={{ backgroundColor: 'green', flex: 1 }}>
{textNodes.map((node)=><Text>{node}</Text>)}
</View>
</View>
<View style={{ height: 10, backgroundColor: "black" }} />
</View>
)
}
export default function App() {
return (
<View style={{ flexDirection: 'row', marginTop: 25 }}>
<Tab textNodes={[1, 2, 3]} />
<Tab textNodes={[1, 2, 3, 4]} />
</View>
);
}
推荐阅读
- javascript - decodeURIComponent 用空格字符串替换标记字符串中的 +
- gpib - 是否有让仪器通过 GPIB 发送测量结果以控制 PC 的协议或明确定义的程序?
- java - 在 API 端点收到 Hibernate @OneToMany Set 时会丢失订单
- python - DjangoRestFramework - 如何使用“pk”过滤列表?
- c# - ThenInclude 语句中某个属性的输出 (EFCore)
- docker - 当我用 docker 构建容器时,我看不到我的 html
- kotlin - 如果在未设置任何内容的情况下局部变量获取默认值,为什么必须初始化获取变量“xxx”?
- oracle-nosql - Oracle NoSQL 数据库中的自增主键
- excel - 搜索具有两个匹配值的行不存在
- django - 从 Django 后端向 Flutter 应用程序发送通知