Jetpack Compose - Column Layout
This example demonstrates how to make a Jetpack Compose Column Layout
Arrangements and Alignments
To set positions of items within a column, we can use verticalArrangement and horizontalAlignment arguments. Similarly, to set positions within a Row, we have the horizontalArrangement and verticalAlignment arguments.
- Center
- Top
- Bottom
- SpcaeEvenly
- SpaceAround
- SpaceBetween
- CenterHorizontally
- Start
- End
package com.compose.example
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.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.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.compose.example.ui.theme.ComposeExampleTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeExampleTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainContent()
}
}
}
}
}
@Composable
fun MainContent() {
Column(
modifier = Modifier
.background(color = Color.LightGray)
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Greeting("AB")
Greeting("CDEF")
Greeting("G")
}
}
@Composable
fun Greeting(name: String) {
Text(
text = "Hello $name!",
fontSize = 32.sp,
fontWeight = FontWeight.Bold,
color = Color.Red,
textAlign = TextAlign.Center,
modifier = Modifier
.background(color = Color.Yellow)
.border(2.dp, color = Color.Green)
.padding(10.dp)
)
}
..
Comments
Post a Comment