Outlined Edit Text from Material Design

时间:2018-09-19 08:16:58

标签: android android-layout android-edittext material-design

How to implement an Outlined text fields presented in Material Design page Activated Edit Text

Link

6 个答案:

答案 0 :(得分:32)

Read Outline Box .

Outline text fields have a stroked border and are less emphasized. To use an outline text field, apply the following style to your TextInputLayout:

 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

dependencies

implementation 'com.android.support:design:28.0.0-alpha1' 

XML

   <android.support.design.widget.TextInputLayout
   android:id="@+id/name_text_input"
   style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   >

   <android.support.design.widget.TextInputEditText
       android:id="@+id/name_edit_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="@string/label_name" />
</android.support.design.widget.TextInputLayout>

DEMO

答案 1 :(得分:9)

更新

也可以和

一起使用
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

使用 implementation 'com.android.support:design:28.0.0-alpha1' 我遇到了错误

  

无法解析符号'@ style / Widget.MaterialComponents.TextInputLayout.OutlineBox'

enter image description here

解决方案

在您的 Build.Gradle

中进行以下更改

使用 compileSdkVersion 28

使用 targetSdkVersion 28

使用以下依赖项

implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support:design:28.0.0-alpha3'

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dangarmahesh.demoapp.MainActivity">

    <ImageView
        android:layout_width="250dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        android:layout_height="250dp" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/userIDTextInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"

        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/userIDTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="User ID" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/passwordTextInputLayout"
        android:layout_margin="10dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/passwordTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password" />

    </android.support.design.widget.TextInputLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:text="LOGIN"
        android:textStyle="bold"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:layout_height="wrap_content" />
</LinearLayout>

OUTOUT

enter image description here

答案 2 :(得分:5)

通过迁移到androidx库,您必须使用新的Material Components for android library

使用TextInputLayout组件:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

并应用此样式:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

enter image description here

要求:

  • 您必须在build.gradle文件中添加this dependencies

    implementation 'com.google.android.material:material:<version>'

  • 应用Material Components theme

    <style name="Theme.MyApp" parent="Theme.MaterialComponents">

答案 3 :(得分:4)

您需要将此依赖项添加到“模块级别” build.gradle com.google.android.material 才能使用最新的 material UI components

然后在您的 com.google.android.material.textfield.TextInputLayout 中使用此样式,

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

here结帐


  

如果您使用的是 com.android.support:design 库,则您   应该将您的应用样式更改为Theme.MaterialComponents...Bridge   (例如,将样式从Theme.AppCompat.Light更改为Theme.MaterialComponents.Light.Bridge

     

首先

     

接下来,您应该在自己的样式中使用此样式    android.support.design.widget.TextInputLayout

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

答案 4 :(得分:2)

如果您使用的是 appcompact 库,则可以使用此 android.support.design.widget.TextInputLayout

如果您使用的是 ANDROIDX 版本,那么我得出一个结论,即根据android jetpack的最新代码。

要使用此功能,您需要在应用程序gradle中具有此依赖项

dependencies {
    implementation 'com.google.android.material:material:1.0.0'
}

然后通过这种方式,您可以将XML添加到UI元素

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/messageTextInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/passwordTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Text here" />

</com.google.android.material.textfield.TextInputLayout>

答案 5 :(得分:2)

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="300dp"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="First Name"/>
 </com.google.android.material.textfield.TextInputLayout>