亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在淺藍色 flexbox 中居中文本?

如何在淺藍色 flexbox 中居中文本?

桃花長相依 2021-11-04 15:19:25
import React, { Component } from 'react';import { View, Text, FlatList, TouchableOpacity, Dimensions, StyleSheet } from 'react-native';const data = [    {id: 'Music', value: 'Music'},    {id: 'Events', value: 'Events'},    {id: 'About Us', value: 'About Us'},    {id: 'Benefits', value: 'Benefits'},    {id: 'Account', value: 'Account'},    {id: 'Social Media', value: 'Social Media'},    {id: 'FAQ', value: 'FAQ'},    {id: 'Settings', value: 'Settings'}  ];const numColumns = 2;const size = Dimensions.get('window').width/numColumns;  export const Grid = () => {    return (        <FlatList          style={{ marginTop: 20 }}          data={data}          renderItem={({item}) => (            <TouchableOpacity style={styles.itemContainer}>              <Text style={styles.item}>{item.value}</Text>            </TouchableOpacity>          )}          keyExtractor={item => item.id}          numColumns={numColumns} />      );}const styles = StyleSheet.create({    itemContainer: {      width: size,      height: size    },    item: {      flex: 1,      margin: 15,      fontSize: 25,      fontWeight: 'bold',      color: 'white',      backgroundColor: 'lightblue'    }  });我嘗試在子視圖和父視圖中使用 justifyContent: "center" 但它不起作用。textAlign: "center" 能夠水平對齊文本。
查看完整描述

3 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

像這樣嘗試。


export const Grid = () => {

    return (

        <FlatList

          style={{ marginTop: 20 }}

          data={data}

          renderItem={({item}) => (

            <TouchableOpacity style={styles.itemContainer}>

              <View style={styles.item}>

                <Text style={styles.itemText}>{item.value}</Text>

              </View>

            </TouchableOpacity>

          )}

          keyExtractor={item => item.id}

          numColumns={numColumns} />

      );

}


const styles = StyleSheet.create({

    itemContainer: {

      width: size,

      height: size

    },

    item: {

      flex: 1,

      justifyContent: 'center',

      alignItems: 'center',

      margin: 15,

      backgroundColor: 'lightblue'

    },

    itemText: {

      fontSize: 25,

      textAlign: 'center',

      fontWeight: 'bold',

      color: 'white',

    }

  });


查看完整回答
反對 回復 2021-11-04
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

您的 itemContainer 類缺少該display: flex屬性。這是在 flex 項目上使用 flexbox 屬性所必需的。


查看完整回答
反對 回復 2021-11-04
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

請附上截圖,詢問有關在 React Native 中進行設計的問題。來到答案你可以試試這個。


item:{

      alignSelf: 'center',

      flex: 1,

      margin: 15,

      fontSize: 25,

      fontWeight: 'bold',

      color: 'white',

      backgroundColor: 'lightblue'

},

itemContainer: {

      width: size,

      height: size,

      justifyContent: 'center',

      alignItems: 'center'

},


查看完整回答
反對 回復 2021-11-04
  • 3 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號