美文网首页
Android Compose (1)基本图文排版小样

Android Compose (1)基本图文排版小样

作者: zcwfeng | 来源:发表于2023-05-10 17:20 被阅读0次

    Sample 说明

    Column/Row 的布局基础
    Image的本地图片使用
    Modifier 布局控制

    package top.zcwfeng.greetingcard
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.Image
    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.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.layout.ContentScale
    import androidx.compose.ui.res.painterResource
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.dp
    import androidx.compose.ui.unit.sp
    import top.zcwfeng.greetingcard.ui.theme.GreetingCardTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                GreetingCardTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colors.background
                    ) {
    
                    }
                }
            }
        }
    }
    
    
    
    @Preview(showBackground = true, showSystemUi = true)
    @Composable
    fun DefaultPreview() {
        GreetingCardTheme {
            BirthdayGreetingWithImage(message = "Happy Birthdy David", from = " - Weimiao")
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String, from: String, modifier: Modifier = Modifier) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = message, fontSize = 36.sp, modifier = Modifier.padding(top = 16.dp))
            Text(
                text = from,
                fontSize = 24.sp,
                modifier = Modifier
                    .padding(top = 16.dp)
                    .align(alignment = Alignment.End)
            )
        }
    }
    
    @Composable
    fun BirthdayGreetingWithImage(message: String, from: String, modifier: Modifier = Modifier) {
        val image = painterResource(R.drawable.androidparty)
        Image(painter = image, contentDescription = null, contentScale = ContentScale.Crop)
        BirthdayGreetingWithText(message = message, from = from)
    
    }
    

    相关文章

      网友评论

          本文标题:Android Compose (1)基本图文排版小样

          本文链接:https://www.haomeiwen.com/subject/wedjsdtx.html