android - React Native marginTop 在 TouchableOpacity 中偏移文本位置
问题描述
所以我刚刚开始在 React Native 中开发并遇到这个问题,由于某种原因,当我尝试将 marginTop 设置为可触摸不透明度时,我的文本不会与可触摸不透明度一起“移动”。这就是我的意思:
这变成:
相关代码如下(第二张图)。对于第一张图片,代码完全相同,只是没有marginTop。这里是:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
export default class App extends Component {
render() {
return (
<TouchableOpacity style={styles.loginButton}>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
loginButton: {
backgroundColor: 'lightblue',
height: '20%',
justifyContent: 'center',
borderRadius: 20,
marginTop: 30%
},
buttonText: {
textAlign: 'center',
fontSize: 20,
},
});
解决方案
use actual pixel number instead of percent number
import {Dimensions} from 'react-native'
const {height} = Dimensions.get('window')
marginTop: 0.3 * height
try and you will get it right
推荐阅读
- python - 将原始 PostgreSQL 转换为 Pyramid 应用程序的 SQLAlchemy ORM 查询
- c++ - 在 operator new 的重载中调用 new 表达式
- batch-file - 如何从文件夹运行 .bat 文件?
- python - 用于记录网页刷新时间的 Python 函数
- javascript - 从字符串中解析 Vector3?
- reactjs - htacess 重定向没有重定向
- swift - Swift:如何在“didSelectRowAt indexPath”部分中隐藏/显示行
- ruby-on-rails - 如何阻止 RSpec 匿名类泄漏?
- php - woocommerce:从续订的订阅 ID 中获取订单 ID
- python - 使用 Python 覆盖 MP3 的艺术尺寸