๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2023 ์•ˆ๋“œ๋กœ์ด๋“œ/์•ˆ๋“œ๋กœ์ด๋“œ ์Šคํ„ฐ๋””

4.Compose ์‚ฌ๋ถ„๋ฉด

by hyeonha 2022. 11. 17.

๋ชฉ์ฐจ

    ์ด๊ฒƒ์„ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

    ์–ด๋ ค์›Œ

    ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ๊ทผ๋ฐ

    modifier = Modifier
        .weight(1f,false).background(Color.Green)
        .padding(top = 16.dp, end = 16.dp, bottom = 16.dp, start = 16.dp)
        .fillMaxWidth(

    ์—ฌ๊ธฐ์„œ weight ์˜ค๋ฅ˜๊ฐ€ ๋œฌ๋‹ค. import๋ฅผ ํ•ด๋„ ์™œ ๋นจ๊ฐ„ ์„ ์ด ์•ˆ์—†์–ด์ง€์ง€........

     

    ์†”๋ฃจ์…˜ ์ฝ”๋“œ

    /*
     * Copyright (C) 2022 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.example.composequadrant
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.background
    import androidx.compose.foundation.layout.Arrangement
    import androidx.compose.foundation.layout.Column
    import androidx.compose.foundation.layout.Row
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.fillMaxWidth
    import androidx.compose.foundation.layout.padding
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Alignment
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.graphics.Color
    import androidx.compose.ui.res.stringResource
    import androidx.compose.ui.text.font.FontWeight
    import androidx.compose.ui.text.style.TextAlign
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.dp
    import com.example.composequadrant.ui.theme.ComposeQuadrantTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                ComposeQuadrantTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(color = MaterialTheme.colors.background) {
                        ComposeQuadrantApp()
                    }
                }
            }
        }
    }
    
    @Composable
    fun ComposeQuadrantApp() {
        Column(Modifier.fillMaxWidth()) {
            Row(Modifier.weight(1f)) {
                ComposableInfoCard(
                    title = stringResource(R.string.first_title),
                    description = stringResource(R.string.first_description),
                    backgroundColor = Color.Green,
                    modifier = Modifier.weight(1f)
                )
                ComposableInfoCard(
                    title = stringResource(R.string.second_title),
                    description = stringResource(R.string.second_description),
                    backgroundColor = Color.Yellow,
                    modifier = Modifier.weight(1f)
                )
            }
            Row(Modifier.weight(1f)) {
                ComposableInfoCard(
                    title = stringResource(R.string.third_title),
                    description = stringResource(R.string.third_description),
                    backgroundColor = Color.Cyan,
                    modifier = Modifier.weight(1f)
                )
                ComposableInfoCard(
                    title = stringResource(R.string.fourth_title),
                    description = stringResource(R.string.fourth_description),
                    backgroundColor = Color.LightGray,
                    modifier = Modifier.weight(1f)
                )
            }
        }
    }
    
    @Composable
    private fun ComposableInfoCard(
        title: String,
        description: String,
        backgroundColor: Color,
        modifier: Modifier = Modifier
    ) {
        Column(
            modifier = modifier
                .fillMaxSize()
                .background(backgroundColor)
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            Text(
                text = title,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(bottom = 16.dp)
            )
            Text(
                text = description,
                textAlign = TextAlign.Justify
            )
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun ComposeQuadrantAppPreview() {
        ComposeQuadrantTheme {
            ComposeQuadrantApp()
        }
    }
    Footer
    © 2022 GitHub, Inc.
    Footer navigation
    Terms
    P

    Column Row Box๋กœ ๊ฐ์‹ธ๋Š”๊ฒŒ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋„ค

    ์ „์ฒด์ ์œผ๋กœ Column์œผ๋กœ ํ•œ๋ฒˆ๋งŒ ๊ฐ์‹ธ๋ฉด ๊ทธ ์•ˆ์˜ ์š”์†Œ๋Š” ๋‹ค ์—ด๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ๋˜๋Š” ๊ฒƒ์ธ๊ฐ€.?

    ์ผ๋‹จ 2๋ฒˆ์ด๋ž‘ 3๋ฒˆ ํ•ด๊ฒฐ๋ชปํ–ˆ๋‹ค.

    -> ๋‹ค์‹œ ํ•  ๊ฒƒ! 

    ๋ธ”๋กœ๊ทธ ๋‚ด์šฉ ๋”ฐ๋ผํ•˜๊ธฐ Modifier 

     

    # fill~ ์ •๋ฆฌ

    Modifier.fillMaxWidth( ) : ๋„ˆ๋น„(๊ฐ€๋กœ) ์ „์ฒด ์ฑ„์šฐ๊ธฐ

    Modifier.fillMaxHeight() : ๋†’์ด(์„ธ๋กœ) ์ „์ฒด ์ฑ„์šฐ๊ธฐ

    Modifier.fillMaxSize( ) : ๋„ˆ๋น„ , ๋†’์ด ๋ชจ๋‘ ์ „์ฒด ์ฑ„์šฐ๊ธฐ, ๊ธฐ๋ณธ์€ ๋ชจ๋‘ ์ฑ„์šฐ๋Š” ๊ฒƒ

    728x90