2016-10-27 36 views
2

我正在尝试构建显示推文的RN组件。我分出了一个很棒的RN组件,react-native-fabric-twitterkit并添加了一个显示推文的组件。到目前为止这么好,但是当我想在ScrollView中显示Tweet时,我必须指定组件的高度和宽度,否则它将不会显示。任何想法我做错了什么?React Native自定义组件不会动态设置组件的大小

这里是我的分叉RN组件:https://github.com/adamivancza/react-native-fabric-twitterkit

这里是一个测试应用程序,展示了我的问题:https://github.com/adamivancza/react-native-twitterkit-test

<View style={styles.container}> 
    <ScrollView 
     style={{ flex: 1 }}> 
     // this component is displayed because I've set a specific height 
     <Tweet 
     style={{ flex: 1, height: 200 }} 
     tweetId='788105828461006848'/> 
     // this component is not displayed 
     <Tweet 
     style={{ flex: 1 }} 
     tweetId='788105828461006848'/> 
    </ScrollView> 
</View> 
+0

你试着将'height'和'width'设置为'null'吗?有一些错误,例如图片在RN – jonzee

+0

@jonzee可悲的是,没有帮助 – Roosevelt

+0

一直有同样的问题。看起来像本地反应有调整意见的问题。 https://github.com/facebook/react-native/issues/4990和https://github.com/facebook/react-native/issues/9275对此问题进行了一些讨论。 – smilledge

回答

1

GitHub的问题通过@smilledge链接有答案。

当在本地组件中动态添加视图时,您似乎需要手动触发布局周期,以便react-native获取更改。 (也许涉及到反应原生更新它的阴影视图)

@Override 
    public void requestLayout() { 
     super.requestLayout(); 
     post(measureAndLayout); 
    } 

    private final Runnable measureAndLayout = new Runnable() { 
     @Override 
     public void run() { 
      measure(
        MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY), 
        MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY)); 
      layout(getLeft(), getTop(), getRight(), getBottom()); 
     } 
    };