繁体   English   中英

将自定义视图与 Jetpack Compose 结合使用

[英]Using Custom Views with Jetpack Compose

假设我正在使用一些旨在提供一些 UI 小部件的库。 假设这个库提供了一个名为FancyButton的 Button Widget。

另一方面,我有一个使用 Android Studio 4 创建的新项目,它允许我创建一个带有Empty Compose Activity的新项目。

问题是:我应该如何将这个FancyButton添加到视图堆栈? 是否可以? 或者对于 Jetpack Compose,我只能使用专门为 Jetpack Compose 开发的组件。 在这种情况下,AFAIK 我只能使用 Android 标准组件( TextMaterialTheme等)。

如果我尝试使用这样的东西:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        MaterialTheme {
            Greeting("Android")
            FancyButton(context, "Some text")
        }
    }
}

然后我得到这个错误:

e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath.

目前(从0.1.0-dev04 ),对此没有好的解决方案。 将来,您将可以简单地将其称为FancyButton("Some text") (不需要context )。

您可以在此处查看 Compose 代码中的演示。

alpha 06更新

可以在可组合中导入Android View实例。

使用ContextAmbient.current作为视图的context参数。

Column(modifier = Modifier.padding(16.dp)) {

    // CustomView using Object
    MyCustomView(context = ContextAmbient.current)

    // If the state updates
    AndroidView(viewBlock = ::CustomView, modifier = modifier) { customView ->
        // Modify the custom view
    }

    // Using xml resource
    AndroidView(resId = R.layout.view_demo)
}

您可以将自定义视图包装在AndroidView 可组合项中:

@Composable
fun RegularTextView() {
    AndroidView(
        factory = { context ->
            TextView(context).apply {
                text = "RegularTextView"
                textSize = 34.dp.value
            }
        },
    )
}

以下是如何在重组期间使用更新参数更新自定义视图:

@Composable
fun RegularTextView() {
    var string by remember {
        mutableStateOf("RegularTextView")
    }
    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        AndroidView(
            factory = { context ->
                TextView(context).apply {
                    textSize = 34.dp.value
                }
            },
            update = { textView ->
                textView.text = string
            }
        )
        Spacer(modifier = Modifier.height(8.dp))
        Button(
            onClick = {
                string = "Button clicked"
            },
        ) {
            Text(text = "Update text")
        }
    }
}
@Composable

fun ButtonType1(text: String,  onClick: () -> Unit) 

{

   Button (
    modifier=Modifier.fillMaxWidth().height(50.dp),
    onClick = onClick,
    shape = RoundedCornerShape(5.dp),
    border = BorderStroke(3.dp, colorResource(id = R.color.colorPrimaryDark)),
    colors = ButtonDefaults.buttonColors(contentColor = Color.White, backgroundColor = colorResource(id = R.color.colorPrimaryDark))
    )
    {
        Text(text = text , color = colorResource(id = R.color.white),
        fontFamily = montserrat,
        fontWeight = FontWeight.Normal,
        fontSize = 15.sp
    )
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM