'RowScopeInstance' にアクセスできません: 'androidx.compose.foundation.layout' の内部にあります 質問する

'RowScopeInstance' にアクセスできません: 'androidx.compose.foundation.layout' の内部にあります 質問する

私は以下のレイアウトを実現しようとしていました

ここに画像の説明を入力してください

使用してみるRow(Modifier.weight(50f))とコンパイラがスローし始めた

輸入元ColumnInstanceimport androidx.compose.foundation.layout.ColumnScopeInstance.weight

Cannot access 'ColumnScopeInstance': it is internal in 'androidx.compose.foundation.layout'

輸入元RowInstanceandroidx.compose.foundation.layout.RowScopeInstance.weight

Cannot access 'RowScopeInstance': it is internal in 'androidx.compose.foundation.layout'

以下にComposableコードを添付します

@Composable
fun BoxLayout(){
    Row(Modifier.weight(50f)) {
        BoxWithText()
        BoxWithText()
    }
}

参照用にファイル全体を添付します

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()
    }
}

なぜエラーが発生するのか分かりません。また、Modifier.weight

質問 -https://developer.android.com/codelabs/basic-android-kotlin-compose-composables-practice-problems?authuser=2&continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-1-pathway-3%3Fauthuser%3D2%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-composables-practice-problems#3

ベストアンサー1

一部の修飾子は、定義されているスコープに固有です。たとえば、Modifier.weight は、デフォルトでは RowScope または ColumnScope でのみ使用できます。または、Modifier.align は BoxScope 内でのみ使用できます。

これらの修飾子にアクセスする場合は、これらのスコープに Composables 関数を配置するか、これらのスコープのレシーバーで @Composable 引数を取る関数を作成する必要があります。

@Composable
fun BoxLayout(){
    Row(Modifier.weight(50f)) {
        BoxWithText()
        BoxWithText()
    }
}

BoxLayoutはModifier.weightを使用できるようにRowScope/ColumnScopeをこのように返す必要があり、これは次のように実行できます。

@Composable
fun BoxWithLayout(content: @Composable RowScope.()->Unit){
    Row {
        content()
    }
}

@Composable
private fun Sample() {
   BoxWithLayout {
       Row(Modifier.weight(50f)) {
           BoxWithText()
           BoxWithText()
       }
   }
}

ここに画像の説明を入力してください

おすすめ記事