马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Android Java 自界说View
步调
创建一个新的Java类,继承自View、ViewGroup或其他任何一个视图类。
假如必要,重写构造函数以支持差异的初始化方式。
重写onMeasure方法以提供精确的丈量逻辑。
重写onDraw方法以实现绘制逻辑。
根据必要重写其他方法,如onSizeChanged、onTouchEvent等。
自界说 View
- public class CustomView extends View {
-
- private Paint paint;
-
- public CustomView(Context context) {
- super(context);
- init();
- }
-
- public CustomView(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- init();
- }
-
- private void init() {
- paint = new Paint();
- paint.setColor(Color.BLUE);
- paint.setStyle(Paint.Style.FILL);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int defaultSize = 200;
- setMeasuredDimension(
- getDefaultSize(defaultSize, widthMeasureSpec),
- getDefaultSize(defaultSize, heightMeasureSpec)
- );
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- // 绘制一个简单的蓝色圆形
- canvas.drawCircle(getWidth() / 2, getHeight() / 2, 100, paint);
- }
- }
复制代码- <com.example.yourapplication.CustomView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
复制代码 Android Java 自界说View
步调
创建一个新的Kotlin类,继承自View或其子类(如TextView, LinearLayout等)。
重写构造函数,至少提供一个可以大概担当Context和AttributeSet的构造函数。
假如必要,重写其他构造函数。
重写onMeasure方法以提供精确的丈量逻辑。
重写onDraw方法以提供绘制逻辑。
根据必要重写其他方法,如onSizeChanged,onTouchEvent等。
自界说View
- import android.content.Context
- import android.graphics.Canvas
- import android.graphics.Color
- import android.graphics.Paint
- import android.util.AttributeSet
- import android.view.View
-
- class CustomView @JvmOverloads constructor(
- context: Context,
- attrs: AttributeSet? = null,
- defStyleAttr: Int = 0
- ) : View(context, attrs, defStyleAttr) {
-
- private val paint = Paint().apply {
- color = Color.RED
- strokeWidth = 5f
- }
-
- override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
- // 设置默认的大小
- val defaultSize = 200
- setMeasuredDimension(
- resolveSize(defaultSize, widthMeasureSpec),
- resolveSize(defaultSize, heightMeasureSpec)
- )
- }
-
- override fun onDraw(canvas: Canvas) {
- super.onDraw(canvas)
- // 绘制一个简单的矩形
- canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
- }
- }
复制代码 ios Object-c 自界说 View
在Objective-C中创建自界说视图通常涉及到继承自UIView类并实现自界说的初始化方法、画图方法和其他必要的方法。以下是一个简朴的自界说视图的例子:
自界说View
- // MyCustomView.h
- #import <UIKit/UIKit.h>
-
- @interface MyCustomView : UIView
-
- @end
-
- // MyCustomView.m
- #import "MyCustomView.h"
-
- @implementation MyCustomView
-
- // 初始化方法,可以自定义或使用默认的初始化方法
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- // 自定义初始化代码
- }
- return self;
- }
-
- // 当视图需要重绘时调用
- - (void)drawRect:(CGRect)rect {
- // 获取图形上下文
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- // 在这里绘制图形或图像
- // 例如,绘制一个简单的红色矩形
- CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
- CGContextFillRect(context, rect);
- }
-
- @end
复制代码- // 在你的视图控制器中
- #import "MyCustomView.h"
-
- // ...
-
- MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
- [self.view addSubview:customView];
复制代码 IOS Swift 自界说 View
在Swift中创建自界说视图通常涉及到界说一个继承自UIView的新类,而且通常必要重写init(frame 和layoutSubviews()方法。以下是一个简朴的自界说视图的例子:- import UIKit
-
- class CustomView: UIView {
- // 自定义视图的初始化
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupView()
- }
-
- // 使用Storyboard或者XIB时需要的初始化方法
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- setupView()
- }
-
- // 视图布局发生变化时调用
- override func layoutSubviews() {
- super.layoutSubviews()
- // 在这里可以根据视图的新布局进行调整
- }
-
- // 设置视图的共有属性
- private func setupView() {
- backgroundColor = .blue // 设置背景颜色为蓝色
- // 其他自定义设置...
- }
-
- // 可以添加更多自定义的方法和属性
- }
复制代码- class ViewController: UIViewController {
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- let customView = CustomView()
- customView.frame = CGRect(x: 50, y: 100, width: 200, height: 200)
- view.addSubview(customView)
- }
- }
复制代码 HarmonyOS Next 自界说View
@Component 表现这是一个自界说组件
@Entry 表现该自界说组件为入口组件
@State 表现组件中的状态变量,状态变量厘革会触发UI革新
UI部门 :以声明式的方式来形貌UI的布局- @Component
- export struct HelloPage {
- @State message: string = 'Hello World'
-
- build() {
- Row() {
- Column() {
- Text(this.message)
- .fontSize(40)
- .fontWeight(FontWeight.Bold)
- .onClick(() => {
- this.message = "Hello Harmonyos"
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
复制代码 React Native 自界说组件
形貌
界说组件布局: 利用 JSX 界说组件的 UI 布局。
界说组件样式: 利用 StyleSheet 或内联样式界说组件的样式。
处置惩罚组件逻辑: 利用 React Hooks(如 useState, useEffect)管理组件状态和副作用。
界说组件接口: 通过 props 通报数据和变乱处置惩罚器,实现组件的可设置性。
自界说组件
- // components/MyButton.js
- import React from 'react';
- import { TouchableOpacity, Text, StyleSheet } from 'react-native';
- const MyButton = ({ title, onPress, style, textStyle }) => {
- return (
- <TouchableOpacity style={[styles.button, style]} onPress={onPress}>
- <Text style={[styles.text, textStyle]}>{title}</Text>
- </TouchableOpacity>
- );
- };
- const styles = StyleSheet.create({
- button: {
- backgroundColor: '#007bff',
- padding: 10,
- borderRadius: 5,
- alignItems: 'center',
- },
- text: {
- color: '#fff',
- fontSize: 16,
- },
- });
- export default MyButton;
复制代码- // screens/HomeScreen.js
- import React from 'react';
- import { View, StyleSheet } from 'react-native';
- import MyButton from '../components/MyButton';
- const HomeScreen = () => {
- const handlePress = () => {
- alert('Button Pressed!');
- };
- return (
- <View style={styles.container}>
- <MyButton title="Press Me" onPress={handlePress} />
- <MyButton title="Custom Style" onPress={handlePress} style={{ backgroundColor: '#28a745' }} textStyle={{ fontSize: 18 }} />
- </View>
- );
- };
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- justifyContent: 'center',
- alignItems: 'center',
- padding: 20,
- },
- });
- export default HomeScreen;
复制代码 Flutter 自界说Widget
形貌
创建一个新的Dart文件。
导入须要的Flutter库。
界说一个继承自StatelessWidget或StatefulWidget的类。
重写build方法以返回一个Widget。
自界说Widget
- import 'package:flutter/material.dart';
-
- class CustomButton extends StatelessWidget {
- final String label;
- final VoidCallback onPressed;
-
- const CustomButton({Key? key, required this.label, required this.onPressed}) : super(key: key);
-
- @override
- Widget build(BuildContext context) {
- return ElevatedButton(
- onPressed: onPressed,
- child: Text(label),
- );
- }
- }
复制代码 CustomButton是一个继承自StatelessWidget的自界说Widget,它担当两个参数:label和onPressed。build方法返回一个ElevatedButton,它是Flutter提供的一个按钮Widget,并利用构造函数中传入的参数。- void main() {
- runApp(MyApp());
- }
-
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- body: Center(
- child: CustomButton(
- label: 'Click Me',
- onPressed: () {
- // Handle button press
- },
- ),
- ),
- ),
- );
- }
- }
复制代码
|