尝试jetpack compose

官网地址: https://developer.android.google.cn/jetpack/compose/interop/adding

初始准备

1.在项目级build.gradle的buildscript中

dependencies {
	classpath "com.android.tools.build:gradle:7.0.3"
	classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
}

2.在module级build.gradle中

android {
    defaultConfig {
        ...
        //minSdkVersion需要大于等于 21
        minSdkVersion 21
        ...
    }

    buildFeatures {
        compose true
    }
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.0.5'
    }
}

dependencies{
	...
	//添加compose 工具包依赖
	// Integration with activities
    implementation 'androidx.activity:activity-compose:1.3.1'
    // Compose Material Design
    implementation 'androidx.compose.material:material:1.0.5'
    // Animations
    implementation 'androidx.compose.animation:animation:1.0.5'
    // Tooling support (Previews, etc.)
    implementation 'androidx.compose.ui:ui-tooling:1.0.5'
    // Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07'
    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.5'
    // When using a MDC theme
    implementation "com.google.android.material:compose-theme-adapter:1.0.5"
    // When using a AppCompat theme
    implementation "com.google.accompanist:accompanist-appcompat-theme:0.16.0"
}

使用compose

1.在新建的activity中使用

override fun onCreate(savedInstanceState: Bundle?) {
	super.onCreate(savedInstanceState)
	//替换之前的setContentView()
    setContent {
    	MaterialTheme {
	        Scaffold(backgroundColor = Color.White) {
            	HelloText()	        
            }
        }
    }
}

@Composable
fun HelloText(){
	Text(text = "hello")
}

2.在已有的activity中使用

2.1 在原有的布局中先用原来的view布局好,在用ComposeView代替原来的view,保留布局参数,id,代码如下

//先用原来的view布局
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:text="我是已有的activity"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <View
        android:id="@+id/v_compose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
//用ComposeView代替原来的view 代替之后代码如下
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        .../>

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/v_compose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2.2 在已有的activity中

override fun onCreate(savedInstanceState: Bundle?) {
	super.onCreate(savedInstanceState)
	setContentView(R.layout.activity_test)
	//先用原来的方法,通过id找到这个view
	val composeView = findViewById<ComposeView>(R.id.v_compose)
	//在用这个view调用setContent来用compose来写页面
	composeView.setContent {
    	MaterialTheme {
    		HelloText()
        }
    }
}

@Composable
fun HelloText(){
	Text(text = "hello 我是compose view")
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值