reactjs - React Native 中的响应式 UI
问题描述
如何在 React Native 中创建响应式 UI 或设计,例如视图(水平和纵向模式)、字体大小等...
并且不更改任何设备中的任何设计或 UI,它在所有设备中都是相同的。
我参考了很多材料,但我并不满意。
解决方案
对于响应式 UI,您可以使用 react-native-responsive-screen 并根据宽度和高度的百分比设计样式
对于响应式字体大小,您可以使用 react-native-responsive-fontsize 并拥有一个单独的组件作为 fonts.js 并在整个应用程序中使用它。
fonts.js 组件可以如下
import { Platform } from "react-native";
import { RFPercentage } from "react-native-responsive-fontsize";
export default fonts={
fontSmall: Platform.OS==='android'? RFPercentage(1.8): RFPercentage(1.5),
fontMedium: RFPercentage(1.8),
fontEntry:RFPercentage(2.0),
fontHeader:RFPercentage(2.1),
fontTV:RFPercentage(2.2),
fontNormal:RFPercentage(2.5),
fontLargeExtra:RFPercentage(2.7),
fontLarge:RFPercentage(3.0),
fontXLarge:RFPercentage(3.5),
fontXXLarge:RFPercentage(4.0),
fontXXLarge:RFPercentage(4.5),
};
组件中的字体组件和响应式 UI 可以按如下方式使用:
import fonts from './fonts';
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
const styles = StyleSheet.create({
viewStyle: {
width: wp('20%'),
marginTop: hp(‘4%’),
height: hp('4.5%'),
fontSize: fonts.fontSmall,
},
});
推荐阅读
- python - ImportError:无法导入名称 BeatifulSoup
- swift - 使用 SwiftyJSON 将 JSON 解析为结构
- javascript - 将前一个兄弟复制到剪贴板
- reactjs - npm run build 内存不足
- loopbackjs - loopbackjs 和 passportConfigurator 护照-facebook
- c# - LINQ 将对象列表分组为匿名类型
- docker - amazonlinux:2 systemctl 未能获得 D-Bus 连接
- php - 通过 api S3 存储桶私有访问
- java - 如何将包含更多详细信息的经纬度地图发送到 Firebase?
- amazon-web-services - Terraform 从另一个项目导入 ECS 任务定义