
简体中文
组件类型:UniNativeViewElement
自定义原生View组件
native-view
自身没有渲染内容,开发者可以通过DOM API获取到native-view
对应的原生view,然后提供平台原生view与native-view
进行绑定,native-view
将展示该view的渲染内容。
<native-view>
组件是uni-app x下扩展原生组件(如map)的重要方式。事实上官方的map组件就是使用<native-view>
开发的。详见下方的使用场景章节。
Web | 微信小程序 | Android | iOS | HarmonyOS |
---|---|---|---|---|
x | x | 4.31 | 4.31 | 4.61 |
名称 | 类型 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|
@init | (event: UniNativeViewInitEvent) => void | - | native-view初始化时回调,event.detail = { element: 'native-view元素实例对象'} |
native-view 组件 init事件event
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
element | UniNativeViewElement | 是 | - | - | - |
native-view
适用于开发uts插件-标准模式组件
native-view
提供 @init 监听元素初始化,通过事件UniNativeViewInitEvent的 detail.element 获取到 UniNativeViewElement。
Android 平台:
UniNativeViewElement 提供bindAndroidView函数与native-view
绑定android平台原生view
IOS 平台:
UniNativeViewElement 提供bindIOSView函数与native-view
绑定ios平台原生view
UniNativeViewElement 提供了dispatchEvent分发event事件API,注意:事件数据类型暂时只支持UniNativeViewEvent。
具体示例请参考:native-button插件,该插件使用native-view
封装原生button实现的native-button。
native-view
组件绑定原生view后自动适配组件全局属性native-view
组件绑定原生view后自动适配组件全局事件 setOnTouchListener
会导致touch部分全局事件失效native-view
组件不支持自定义属性,使用uts插件-标准模式组件-声明属性props实现自定义属性目的native-view
组件不支持子组件native-view
组件不支持list-item复用机制,list-item其他子组件不受影响正常启动复用业务。native-view
组件不支持background、border、boxshadow属性native-view
组件不支持overflow属性设置visible,仅支持hidden不可以嵌套组件
NativeButton原生对象代码如下:
Android
iOS
import { Button } from "android.widget"
export class NativeButton {
$element: UniNativeViewElement;
constructor(element: UniNativeViewElement) {
this.$element = element;
this.bindView();
}
button: Button | null = null;
bindView() {
//通过UniElement.getAndroidActivity()获取android平台activity 用于创建view的上下文
this.button = new Button(this.$element.getAndroidActivity()!); //构建原生view
//限制原生Button 文案描述不自动大写
this.button?.setAllCaps(false)
//监听原生Button点击事件
this.button?.setOnClickListener(_ => {
const detail = {}
//构建自定义UniNativeViewEvent返回对象
const event = new UniNativeViewEvent("customClick", detail)
//响应分发原生Button的点击事件
this.$element.dispatchEvent(event)
})
//UniNativeViewEvent 绑定 安卓原生view
this.$element.bindAndroidView(this.button!);
}
updateText(text: string) {
//更新原生Button 文案描述
this.button?.setText(text)
}
destroy() {
//数据回收
}
}
具体示例请参考:native-button插件