TextInputLayout使用方式之修改样式

最近做的项目使用到了TextInputLayout这个控件。它是desig包下的。

material design是Android5.0之后引入的。它新增了一些控件。有需要了解的朋友请自行翻阅资料。下面是它的依赖

compile 'com.android.support:design:27.0.2'

TextInputLayout控件继承LinearLayout,使用时包含且只能包含一个EditText输入框。它可以显示浮动效果。它的很多属性还是跟EditText一样。但是我在开发时,因项目需要将默认的hint颜色,下划线颜色,点击后的颜色都需要修改。它默认是这种颜色的,看下图

这种是默认样式,它产生了一种浮动效果,看起来非常炫酷。

它的代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:paddingLeft="@dimen/forty_dp"
    android:paddingRight="@dimen/forty_dp"
    android:paddingTop="@dimen/one_hundred_twenty_dp"
    tools:context="com.example.administered.swiperefreshlayouttest.view.activitys.MainActivity">
    
    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_textinput_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        
        <EditText
            android:id="@+id/login_userId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true"
             />
        
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_textinput_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_textinput_id"
        android:layout_marginTop="@dimen/five_dp"
        >

        <EditText
            android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
             />
       
    </android.support.design.widget.TextInputLayout>
    
    <Button
        android:id="@+id/sign_in_button"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_textinput_pwd"
        android:layout_marginTop="@dimen/twenty_dp"
        android:background="@drawable/btn_nor_up"
        android:text="登录"
        android:textStyle="bold" />

</LinearLayout>

我因为项目需要,需要将默认下划线颜色,hint等颜色需要做改变,因为背景要变成深色。这种默认的颜色,在深色背景下有点看不清楚。

做了如下修改

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@color/skyblue"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:paddingLeft="@dimen/forty_dp"
    android:paddingRight="@dimen/forty_dp"
    android:paddingTop="@dimen/one_hundred_twenty_dp"
    tools:context="com.example.administered.swiperefreshlayouttest.view.activitys.MainActivity">

    <!--改变TextInputLayout默认下划线颜色和点击时颜色,默认hint颜色
           在TextInputLayout控件设置textColorHint设置默认hint颜色
           在EditText控件中设置android:theme属性设置 style
       -->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_textinput_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/mintcream">
        <!--android:textColorHint="@color/blue" 默认hint字体颜色-->
        <EditText
            android:id="@+id/login_userId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="@color/darkorange"
            android:theme="@style/TextAppTheme" />
        <!--textColor设置输入时字体颜色-->
        <!--inputTypu设置输入框输入类型-->

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

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_textinput_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_textinput_id"
        android:layout_marginTop="@dimen/five_dp"
        android:textColorHint="@color/mintcream">

        <EditText
            android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="@color/darkorange"
            android:theme="@style/TextAppTheme" />

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


    <Button
        android:id="@+id/sign_in_button"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_textinput_pwd"
        android:layout_marginTop="@dimen/twenty_dp"
        android:background="@drawable/btn_nor_up"
        android:text="登录"
        android:textStyle="bold" />

</LinearLayout>

修改的代码我做了注释,TextAppTheme.style代码如下

<!--改变TextInputLayout 里面的EditText默认下划线和点击时下划线的颜色-->
    <style name="TextAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorControlNormal">@color/chartreuse</item><!--默认显示下划线的颜色-->
        <item name="colorControlActivated">@color/orangered</item><!--点击后下划线的颜色-->
        <item name="colorAccent">@color/ivory</item>
    </style>

效果如下

其实就是修改了一些样式而已,它的其他的一些属性,暂时还没发掘出来。还需要学习

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要改变AndroidTextInputLayout样式,可以在styles.xml文件中定义一个新的样式,然后将它应用到TextInputLayout控件上。 例如,要更改TextInputLayout的边框颜色和标签颜色,可以按照以下步骤操作: 1.在styles.xml文件中定义一个新的样式: ```xml <style name="MyTextInputLayoutStyle" parent="Widget.Design.TextInputLayout"> <!-- 边框颜色 --> <item name="boxStrokeColor">@color/my_color</item> <!-- 标签颜色 --> <item name="android:textColorHint">@color/my_color</item> </style> ``` 2.将新样式应用到TextInputLayout控件上: ```xml <com.google.android.material.textfield.TextInputLayout android:id="@+id/my_text_input_layout" style="@style/MyTextInputLayoutStyle" android:layout_width="match_parent" android:layout_height="wrap_content" app:hint="My Hint"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout> ``` 这样就可以更改TextInputLayout的边框颜色和标签颜色了。您可以根据需要更改其他属性。 ### 回答2: AndroidTextInputLayout是一个扩展的布局控件,用于实现漂亮的文本输入框。要改变其样式,我们可以通过以下步骤进行操作: 1. 首先,在项目的build.gradle文件中添加新的依赖项: ``` implementation 'com.google.android.material:material:<版本号>' ``` 请注意,这里的版本号可以根据你的项目需要进行调整。 2. 在布局文件中,将TextInputLayout作为容器包裹EditText控件,并设置所需属性,如hint、hintTextColor等。例如: ```xml <com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" app:hintTextColor="@color/colorAccent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout> ``` 3. 使用代码动态修改样式。在Activity或Fragment中,可以通过findViewById方法获取到TextInputLayout对象,并设置所需的样式。例如,改变文本颜色: ```java TextInputLayout textInputLayout = findViewById(R.id.textInputLayout); textInputLayout.setHintTextColor(Color.RED); ``` 以上是使用AndroidTextInputLayout改变样式的基本步骤。通过设置不同的属性和使用代码动态修改样式,可以实现丰富多样的UI效果,以满足实际需求。 ### 回答3: 要改变Android TextInputLayout样式,可以通过以下步骤: 1. 首先,在项目的res文件夹下创建一个新的XML文件,比如命名为custom_text_input_layout.xml。在该文件中,可以自定义TextInputLayout样式。 2. 在custom_text_input_layout.xml文件中,可以使用XML属性来改变样式。例如,可以修改背景颜色、边框颜色、字体颜色等。可以根据自己的需求进行样式修改。 3. 在布局文件中使用自定义样式TextInputLayout。找到需要使用TextInputLayout控件,通过设置style属性将其样式修改为custom_text_input_layout。例如:<com.google.android.material.textfield.TextInputLayout style="@style/CustomTextInputLayout"> 4. 如果还需要修改TextInputEditText(即EditText控件),可以在CustomTextInputLayout的属性中设置相应的样式,例如:app:hintTextAppearance="@style/CustomTextInputEditText" 5. 另外,还可以通过在styles.xml文件中定义样式,再将样式应用到CustomTextInputLayout中。例如,在styles.xml中定义一个样式: <style name="CustomTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox"> <item name="boxBackgroundMode">filled</item> <item name="boxBackgroundColor">@color/custom_background_color</item> <item name="hintTextColor">@color/custom_hint_text_color</item> <item name="boxStrokeColor">@color/custom_stroke_color</item> </style> 然后,在布局文件中使用样式:<com.google.android.material.textfield.TextInputLayout style="@style/CustomTextInputLayout"> 通过以上步骤,就可以自定义Android TextInputLayout样式了。根据自己的需求,可以修改背景颜色、边框颜色、字体颜色等来改变样式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值