San*_*esh 19 android android-layout android-studio android-jetpack-compose
我试图实现以下布局
我尝试使用Row(Modifier.weight(50f))编译器开始抛出的错误
如果进口自ColumnInstance-import androidx.compose.foundation.layout.ColumnScopeInstance.weight
Cannot access 'ColumnScopeInstance': it is internal in 'androidx.compose.foundation.layout'
如果进口自RowInstance-androidx.compose.foundation.layout.RowScopeInstance.weight
Cannot access 'RowScopeInstance': it is internal in 'androidx.compose.foundation.layout'
下面附上我的可组合代码
@Composable
fun BoxLayout(){
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
Run Code Online (Sandbox Code Playgroud)
附上整个文件以供参考
package me.sanjaykapilesh.layoutmastery
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScopeInstance.weight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import me.sanjaykapilesh.layoutmastery.ui.theme.LayoutMasteryTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LayoutMasteryTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
BoxWithText()
}
}
}
}
}
@Composable
fun BoxLayout(){
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
@Composable
fun BoxWithText() {
Column() {
Text(text = "Hello Box!")
Text(text = "Displays text and follows Material Design guidelines")
}
}
@Preview(showBackground = true)
@Composable
fun BoxLayoutPreview() {
LayoutMasteryTheme {
BoxLayout()
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我会收到错误。我也无法实现Modifier.weight
Hor*_*tio 17
您可以使用扩展函数来获取上下文。例如:
@Composable
fun ColumnScope.BoxLayout(){
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
Run Code Online (Sandbox Code Playgroud)
Thr*_*ian 16
某些修饰符对于定义它们的作用域是唯一的,例如 Modifier.weight 默认情况下仅在 RowScope 或 ColumnScope 中可用。或者 Modifier.align 仅在 BoxScope 内可用。
当您希望访问这些修饰符时,您需要在这些范围内拥有可组合函数,或者创建一个带有这些范围的接收器的 @Composable 参数的函数
@Composable
fun BoxLayout(){
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
Run Code Online (Sandbox Code Playgroud)
BoxLayout 应返回 RowScope/ColumnScope,因为这样才能使用 Modifier.weight,并且可以这样做
@Composable
fun BoxWithLayout(content: @Composable RowScope.()->Unit){
Row {
content()
}
}
@Composable
private fun Sample() {
BoxWithLayout {
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
需要在任何行/列范围内调用权重参数。所以试试这个
@Composable
fun BoxLayout(){
Row{
Row(Modifier.weight(50f)) {
BoxWithText()
BoxWithText()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18148 次 |
| 最近记录: |