package com.abc.robot01.activity.compose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
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.res.painterResource
import androidx.compose.ui.unit.dp
import com.abc.robot01.R
class AlignmentDemo : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val scrollState = rememberScrollState()
Column(modifier = Modifier
.verticalScroll(scrollState)
.fillMaxSize()) {
BoxAlignmentExample()
Spacer(modifier = Modifier.height(2.dp))
BoxTopStartAlignmentExample()
Spacer(modifier = Modifier.height(2.dp))
BoxBottomEndAlignmentExample()
Spacer(modifier = Modifier.height(2.dp))
TwoImagesInBox()
Spacer(modifier = Modifier.height(2.dp))
} }
}
}
@Composable
fun BoxAlignmentExample() {
Box(
modifier = Modifier
.size(200.dp)
.background(Color.LightGray),
contentAlignment = Alignment.Center
) {
Text(text = "Centered", color = Color.Black)
}
}
@Composable
fun BoxTopStartAlignmentExample() {
Box(
modifier = Modifier
.size(200.dp)
.background(Color.LightGray),
contentAlignment = Alignment.TopStart
) {
Text(text = "Top Start", color = Color.Black)
}
}
@Composable
fun BoxBottomEndAlignmentExample() {
Box(
modifier = Modifier
.size(200.dp)
.background(Color.LightGray),
contentAlignment = Alignment.BottomEnd
) {
Text(text = "Bottom End", color = Color.Black)
}
}
@Composable
fun TwoImagesInBox() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Gray) // 背景色用于示例
) {
// Image on the left
Image(
painter = painterResource(id = R.drawable.close), // 替换为你的图片资源
contentDescription = "Left Image",
modifier = Modifier
.align(Alignment.CenterStart)
.padding(start = 16.dp)
.size(100.dp) // 设置图片大小
)
// Image on the right
Image(
painter = painterResource(id = R.drawable.open), // 替换为你的图片资源
contentDescription = "Right Image",
modifier = Modifier
.align(Alignment.CenterEnd)
.padding(end = 16.dp)
.size(100.dp) // 设置图片大小
)
}
}
07-29
589
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-24
1634
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-15
337
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-07