简体   繁体   English

React-Native Native UI 组件不调整大小(Android)

[英]React-Native Native UI component doen't resize (Android)

I have issues with React Native respecting the height of an Android Native UI Component.我对 React Native 有关于 Android Native UI 组件高度的问题。 I have created a very simple use-case to demonstrate the issue.我创建了一个非常简单的用例来演示这个问题。

React Native version = 0.61.5. React Native 版本 = 0.61.5。

Android, React Native ViewManager: Android,React Native ViewManager:

public class CustomTextViewManager extends SimpleViewManager<TextView> {

    @NonNull
    @Override
    protected TextView createViewInstance(@NonNull ThemedReactContext reactContext) {
        return new TextView(reactContext);
    }

    @NonNull
    @Override
    public String getName() {
        return "CustomTextView";
    }

    @ReactProp(name = "text")
    public void setText(TextView view, String text) {
        view.setText(text);
    }
}

JavaScript, native view reference: JavaScript,原生视图参考:

import {requireNativeComponent} from 'react-native';

const CustomTextView = requireNativeComponent('CustomTextView')

export default CustomTextView;

JavaScript, simple app: JavaScript,简单的应用程序:

import React from 'react';
import CustomTextView from './CustomTextView';

const App: () => React$Node = () => {
  return (
    <CustomTextView
      text={'Some test text'}
    />
  );
};

export default App;

When you run this code nothing is shown.当您运行此代码时,不会显示任何内容。 After adding style={{height: 100}} the view is shown at the provided fixed height.添加 style={{height: 100}} 后,视图以提供的固定高度显示。

I actually expected that the height of the Android View would be reflected in React Native.我实际上预计 Android View 的高度会反映在 React Native 中。 Apparently this is not the case.显然情况并非如此。

Note: height: "auto" doesn't work.注意:高度:“自动”不起作用。 I hope that someone can tell me what I'm missing.我希望有人能告诉我我错过了什么。

I faced the same problem recently.我最近遇到了同样的问题。 Your need to use a shadow node to measure the final size which the TextView will have after the text was rendered.您需要使用阴影节点来测量 TextView 在文本渲染后的最终大小。 I found a good article about this.我找到了一篇关于这个的好文章。 click me 点我

If your TextView will render simple text only, you can just use the ReactTextShadowNode which is part of the native library of react native.如果您的 TextView 将只渲染简单的文本,您可以只使用 ReactTextShadowNode,它是 react native 的本地库的一部分。

To do so just override the method createShadowNodeInstance of your ViewManager and return an instance of the ReactTextShadowNode.为此,只需覆盖 ViewManager 的 createShadowNodeInstance 方法并返回 ReactTextShadowNode 的实例。

    override fun createShadowNodeInstance(): ReactTextShadowNode {
        return ReactTextShadowNode(null)
    }

If your TextView has to render some special Spanns, you need to write your own implementation of ShadowNode.如果你的 TextView 必须渲染一些特殊的 Span,你需要编写自己的 ShadowNode 实现。 I did so by coping the ReactTextShadowNode and changed the way how the field mPreparedSpannableText was bild.我通过处理 ReactTextShadowNode 并改变了字段 mPreparedSpannableText 的方式来做到这一点。 This field holds the span which will be used to measure the desired height.该字段保存将用于测量所需高度的跨度。

eg.例如。 you could use something like that你可以使用类似的东西

    @ReactProp(name = "text")
    fun setText(text: String?) {
        this.text = text ?: ""
        mPreparedSpannableText = useYourMethodToCreateYourSpannable()
        dirty()
    }

This post was quiet old but I hope my answer will help someone facing the same problem.这篇文章很老,但我希望我的回答能帮助面临同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native本机ui组件android中具有自定义名称的事件 - Events with Custom names in react-native native ui component android 多行宽度 React-Native 文本组件不会调整为 Android 上最长文本行的长度 - Width of multiline React-Native Text component won't resize to length of longest line of text on Android 条码扫描器反应原生 android UI 组件未显示 - Barcode scanner react-native android UI component not showing up 循环一个React-Native UI组件 - Loop a React-Native UI component React-Native Android:将Object作为Native Android UI Component的prop - React-Native Android: Pass Object as prop for Native Android UI Component React Native:调整自定义 UI 组件的大小 - React Native: Resize custom UI component React-Native Android:调整本机UI组件的大小会导致停电 - React-Native Android: Resizing native UI Component causes black out 如何在本机Android组件的React-Native中确定DevMode? - How to determine DevMode in React-Native in Native Android Component? 在Android中的自定义本机UI组件react-native 0.21.0中,如何将属性从react-native传递给ViewManager中的setter方法? - In a custom native UI component in Android, react-native 0.21.0, how can I pass properties from react-native to the setter methods in a ViewManager? React Native:使用JWPlayer的原生Android UI组件不显示视频图像 - React Native: Native Android UI component with JWPlayer shows no video image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM