android - 如何在android studio项目中使用材料设计
问题描述
我正在尝试在我的 android 项目中使用材料设计文本字段。
问题
当我在我的代码中使用TextInputLayout
and时,我收到一个错误,即无法识别或找到这两个类。TextInputEditText
xml
我尝试导入设计支持库,但没有奏效。
问题
如何在我的 android 项目中使用材料设计文本字段?
我的目标 SDK 版本是 28
这是我的xml代码
<android.support.constraint.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:background="@android:color/background_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/pickImgBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick Image"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
app:layout_constraintTop_toTopOf="parent"
style="?android:attr/borderlessButtonStyle"
android:onClick="pickImage" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textfieldContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/pickImgBtn">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/toptextfield"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="add top text"
android:padding="10dp"
android:layout_margin="10dp" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/bottomtextfield"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="add top text"
android:padding="10dp"
android:layout_margin="10dp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/createMemeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Meme"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toBottomOf="@id/textfieldContainer"
style="?android:attr/borderlessButtonStyle" />
<android.support.constraint.ConstraintLayout
android:id="@+id/memeLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="51dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/createMemeBtn">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:color/darker_gray"/>
<TextView
android:id="@+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Top Text Here"
android:layout_weight="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#fff"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/bottomtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Insert Bottom Text Here"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
<Button
android:id="@+id/saveMemeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Image"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toBottomOf="@id/memeLayout"
style="?android:attr/borderlessButtonStyle" />
</android.support.constraint.ConstraintLayout>
解决方案
您需要使用以下代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email" />
</android.support.design.widget.TextInputLayout>
如果要使用材质文本字段 ,则需要添加材质库
implement com.google.android.material:material:1.0.0-beta01
注意:您不应该同时在您的应用程序中使用
com.android.support
和com.google.android.material
依赖项。请在此处阅读说明
推荐阅读
- javascript - 识别给定的时间戳
- python - 给定条件,修改数据框的值
- javascript - 使用预先存在的 redux 存储实现固定数据表 2 排序?
- firebase - Firestore 安全规则获取功能错误
- symfony - 如何使用 Symfony HttpClient 发布数据
- go - 单值上下文中的多值错误返回一个变量和一个多返回函数
- jquery - 在保存/发布 Gutengerg (WordPress) 中的块之前使用条件触发 Javascript 函数
- ios - 通用链接 iOS - 支持来自单个域的多个捆绑 ID
- django-rest-framework - 是否可以使用 django-simple-history 创建具有所有历史记录的序列化程序?
- git - 如何使 Go 模块语义导入版本控制 v2+ 与虚荣导入路径一起使用