Material Design Components正式的1.0.0推出了,來看看其中的TextInputLayout,其中最顯著的效果就是當使用者在EditText輸入文字時,原先hint的文字會跑到較上方的位置,如下圖↓
首先要使用的話,在build.gradle(app)中加上
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
implementation 'com.google.android.material:material:1.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<com.google.android.material.textfield.TextInputLayout | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="32dp" | |
android:layout_marginEnd="32dp" | |
android:gravity="center_vertical" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent"> | |
<com.google.android.material.textfield.TextInputEditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/edit_hint_text" /> | |
</com.google.android.material.textfield.TextInputLayout> | |
</androidx.constraintlayout.widget.ConstraintLayout> |