如何将ProgressBar置于屏幕中央并移除React Native中的警告消息

4
如何将进度条居中显示,就像在相对布局的父元素中居中显示Android中一样,并且如何删除警告消息。如何更改ProgressBar的可见性(不可见或已消失或可见)。 MyScreen
var ProgressBar = require('ProgressBarAndroid');
<View style={{ flex: 1, backgroundColor: 'steelblue', padding: 10, justifyContent: 'center' }}>
        <Text style={{
            backgroundColor: 'steelblue', justifyContent: 'center', alignItems: 'center', fontSize: 40, textAlign: 'center',
            color: 'white', marginBottom: 30
        }}>LOGIN</Text>
        <ProgressBar style={{justifyContent:'center',alignItems:'center',styleAttr:'LargeInverse', color:'white'}} />
        <View style={{ justifyContent: 'center' }}>
            <TextInput
                style={{ height: 50, marginLeft: 30, marginRight: 30, marginBottom: 20, color: 'white', fontSize: 20 }}
                placeholder='Username' placeholderTextColor='white'
                autoFocus={true}
                returnKeyType='next'
                keyboardType='email-address'
                onChangeText={(valUsername) => _values.username = valUsername}
                />
            <TextInput
                secureTextEntry={true}
                style={{ height: 50, marginLeft: 30, marginRight: 30, marginBottom: 20, color: 'white', fontSize: 20 }}
                placeholder='Password' placeholderTextColor='white'
                onChangeText={(valPassword) => _values.password = valPassword}
                />
        </View>
        <Button onPress={() => { _handlePress() } } label='Login' />
        <View>
            <Text style={{ color: 'white', justifyContent: 'center', textAlign: 'center', alignItems: 'center', fontSize: 20, textDecorationLine: 'underline', textDecorationStyle: 'solid' }}>
                Forgot Password
            </Text>
            <Text style={{ color: 'white', marginTop: 10, justifyContent: 'center', textAlign: 'center', alignItems: 'center', fontSize: 20, textDecorationLine: 'underline', textDecorationStyle: 'solid' }}>
                SignUp
            </Text>
        </View>
    </View>

尝试使用ActivityIndicator代替progressbar。另外,您所说的屏幕中心是什么意思?作为覆盖层吗? - Ravi Theja
是的,就像在中心的覆盖层一样,如何改变它的可见性。 - YLS
1
请参考以下链接:https://dev59.com/MF0a5IYBdhLWcg3wLV8G#30870639 - Ravi Theja
没有,我的进度条没有居中显示在屏幕上。 - YLS
1个回答

1
正如 @ravi-teja 所指出的那样,使用绝对定位。此外,您之所以会收到警告,是因为 styleAttr 不是样式属性(名称具有误导性),而是一个 prop。您还可以使用布尔值(myCondition)在程序中编程地显示和隐藏它(您可能可以将其存储在状态中)。您应该这样做:
var ProgressBar = require('ProgressBarAndroid');

// ...
render(){
    const {width, heigth} = Dimensions.get('window'); // The dimensions may change due to the device being rotated, so you need to get them in each render.
    return (

       //...
       {this.state.myCondition && <ProgressBar styleAttr={'LargeInverse'} style={{position: 'absolute', left: width/2, height: height/2, justifyContent:'center',alignItems:'center', color:'white'}} />}
       //...
}

除此之外,正如其他人所说,你应该尝试使用ActivityIndicator,因为它适用于iOS和Android。

初始状态下,我的条件为false。当我将mycondition更改为true时,我无法在屏幕上看到ActivityIndicator(进度条),该如何刷新以显示指示器。我尝试了您的方法,通过设置绝对值和其他方式,但我的activityindicator仍然显示在屏幕左侧的末尾。上面我已经发布了我的屏幕和视图代码。 - YLS
你是否使用 this.setState({myCondition: true}) 来更新值?否则它不会触发渲染。尝试不使用 justifyContentalignItems 样式。 - martinarroyo
我没有使用state,而是使用const _showProgress = { progress: false }。有没有其他方法可以在不使用state的情况下进行刷新? - YLS
有一些方法(例如使用基于流的模式),但最简单的情况是使用状态。React 的思想是组件始终与其 props 和 state 同步,因此触发渲染的唯一方式是更新其中之一。 - martinarroyo
这是我的样例项目登录界面,由于它并不是一个类,我该如何在其中添加props或state?请问你能帮我看看该如何添加state吗? - YLS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接