jetpack_探索jetpack构成基础知识

jetpack

Jetpack Compose, the next big change in Android is now in the alpha stage, so this might be the perfect time to start exploring the library and find out how it will help us writing better user interfaces with ease.

Jetpack Compose是Android的下一个重大变化,现在正处于alpha阶段,因此这可能是开始探索该库并了解它将如何帮助我们轻松编写更好的用户界面的绝佳时机。

Jetpack Compose is Android’s modern toolkit for building native UI. It simplifies and accelerates UI development on Android. — Official Docs.

Jetpack Compose是Android用来构建本机UI的现代工具包。 它简化并加速了Android上的UI开发。 - 官方文档

使用撰写的好处 (Benefits of Using Compose)

  • Jetpack compose eliminates us writing long XML files. The same UI can be created with less amount of declarative code.

    Jetpack compose消除了我们编写长XML文件的麻烦。 可以使用更少的声明性代码来创建相同的UI。
  • Jetpack compose automatically handles the change in UI state when the data in the app changes.

    当应用程序中的数据发生更改时,Jetpack compose会自动处理UI状态的更改。
  • It is compatible with the existing code in our app. You can easily migrate to compose.

    它与我们应用程序中的现有代码兼容。 您可以轻松地进行迁移。
  • It has built in support for material design, animations, dark theme etc.

    它内置了对材料设计,动画,深色主题等的支持。
  • It has direct access to the Android platform APIs.

    它可以直接访问Android平台API。

项目设置 (Project Setup)

Since the Jetpack Compose is in the alpha stage, you need to use the latest canary version of android to work on it. In the canary version of android studio you can start a compose project by choosing an Empty Compose Activity.

由于Jetpack Compose处于Alpha阶段,因此您需要使用最新的Canary版本的android进行处理。 在android studio的金丝雀版本中,您可以通过选择一个空的撰写活动来启动撰写项目

  • Your minSdkVersion should not be lower than 21.

    您的minSdkVersion不得低于21。

  • Make sure you use the kotlin version 1.4.0 or higher.

    确保您使用的Kotlin版本1.4.0或更高版本。

Android studio will automatically configure your project to use compose by adding required dependencies to your app level build.gradle.

通过将必需的依赖项添加到应用程序级别build.gradle Android studio将自动将您的项目配置为使用compose。

build.gradle
build.gradle

一些基本的事情 (Some Basic Things)

Jetpack Compose completely changes the way we create user interfaces in android, which means we will create UI not by writing XML but by describing our UI in the application code.

Jetpack Compose完全改变了我们在android中创建用户界面的方式,这意味着我们将不通过编写XML而是通过在应用程序代码中描述UI来创建UI。

可组合功能 (Composable Functions)

Composable functions are the heart of Jetpack Compose. In Jetpack Compose you will declare your app UI components such as Button, TextView, EditText etc. programmatically by creating composable functions rather that writing the views in XML files. You can create a composable function by marking it with @Composable annotation.

可组合功能是Jetpack Compose的核心。 在Jetpack Compose中,您将通过创建可组合函数(而不是将视图写入XML文件)以编程方式声明您的应用程序UI组件,例如ButtonTextViewEditText等。 您可以通过使用@Composable批注对其进行标记来创建可组合函数。

MainActivity.kt
MainActivity.kt
  • The SayHello() function is a composable function marked with the @Composable annotation.

    SayHello()函数是标有@Composable批注的可组合函数。

  • The Text() function is an another composable function provided in the jetpack compose api. It creates a text element in the activity.

    Text()函数是jetpack compose api中提供的另一个可组合函数。 它在活动中创建一个文本元素。

  • The setContent{ } is a function provided by the jetpack compose which composes the given composable into the given activity. The composable passed in it becomes the root view of the activity.

    setContent{ }是jetpack compose提供的功能,它将给定的组合物组合到给定的活动中。 传递给它的composable成为活动的根视图。

  • Composable functions can only be called from other composable functions.

    可组合函数只能从其他可组合函数中调用。

The above code will create a text element in the top left corner of the activity.

上面的代码将在活动的左上角创建一个text元素。

Similarly you can create a Button element using the Button() composable function.

同样,您可以使用Button()可组合函数创建一个Button元素。

  • The Buttton() composable function takes a onClick lambda for the button click event.

    Buttton()可组合函数对按钮click事件采用onClick lambda。

  • The Button() function takes an another lambda for the content shown on the Button such as the text “Show Toast”, which we create by creating a text element with the Text() function.

    Button()函数需要另一个lambda来表示Button上显示的内容,例如文本“ Show Toast”,我们通过使用Text()函数创建文本元素来创建该文本。

The above code creates a Button in the top left corner of the activity. Clicking that button shows a toast message.

上面的代码在活动的左上角创建一个Button。 单击该按钮将显示一条祝酒消息。

版面 (Layouts)

If you create multiple text element with the Text() function as described above, they will appear on top of each other because their is no layouts associated with the text elements. The jetpack compose api provides functions to create layouts to stack UI elements vertically or horizontally in the activity.

如果如上所述使用Text()函数创建多个文本元素,则它们将彼此重叠,因为它们没有与文本元素关联的布局。 jetpack compose api提供了一些功能来创建布局以在活动中垂直或水平堆叠UI元素。

To stack the elements vertically, you need to use the Column function, and to stack the elements horizontally, you need to use the Row function.

要垂直堆叠元素,需要使用Column函数,而要水平堆叠元素,则需要使用Row函数。

MainActivity.kt
MainActivity.kt
  • The Column function stacks the elements vertically

    Column函数垂直堆叠元素

  • The Row function stacks the elements horizontally.

    Row功能将元素水平堆叠。

Image for post
The text “Tuesday” and “3:09 pm” stacked horizontally
文本“星期二”和“下午3:09”水平堆叠

修饰符 (Modifiers)

You can decorate a composable with the use of modifiers. By using modifiers you can change the appearance of a composable like adding padding, changing size, applying gravity etc. or you can make an element clickable, scrollable or zoomable.

您可以使用修饰符装饰可组合对象。 通过使用修饰符,您可以更改可编辑对象的外观,例如添加填充,更改大小,应用重力等,也可以使元素可单击,滚动或缩放。

Applying modifiers to a text element
将修饰符应用于文本元素

You can apply a padding to the start of the text element with the Modifier.padding(start = 8.dp).

您可以使用Modifier.padding(start = 8.dp)将填充应用于文本元素的Modifier.padding(start = 8.dp)

创建一个UI组件 (Creating a UI Component)

Now we will use the above concepts to create a user card UI component as described in the official codelab.

现在,我们将使用以上概念来创建用户卡UI组件,如官方代码实验室中所述

Image for post
User card UI component
用户卡UI组件
MainActivity.kt
MainActivity.kt
  • The Row function stacks the user profile image and the name horizontally.

    Row功能可水平堆叠用户个人资料图像和名称。

  • The Column function stacks the user name and job vertically.

    Column函数垂直堆叠用户名和作业。

  • The Surface function acts as a placeholder for the profile image. We can apply the shape and color of the surface with the shape and color property and we can set the size with .preferredSize() modifier.

    Surface功能充当轮廓图片的占位符。 我们可以使用shapecolor属性应用表面的shapecolor并可以使用.preferredSize()修饰符设置尺寸。

  • You can apply spacing between the profile image and user detail by adding a padding with the .padding() modifier.

    您可以通过使用.padding()修饰符添加填充来在个人资料图像和用户详细信息之间应用间距。

  • The ProvideEmphasis() creates an emphasis to the text element.

    ProvideEmphasis()将重点放在text元素上。

  • You can set the whole component clickable with the .clickable() modifier and set the shape of the ripple with the .clip() modifier.

    您可以使用所设置的整个组件可点击.clickable()修改并设置波纹的形状与.clip()修改。

It’s done. You learned and created a basic UI component with Jetpack compose. In the next article we will learn some more concepts such as applying material theme and creating complex layouts by creating some screens in jetpack compose.

完成。 您通过Jetpack compose学习并创建了一个基本的UI组件。 在下一篇文章中,我们将学习更多概念,例如应用材料主题和通过在jetpack compose中创建一些屏幕来创建复杂的布局。

翻译自: https://medium.com/dev-genius/exploring-jetpack-compose-the-basics-cfb27264e6c6

jetpack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值