首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在TextInputLayout非焦点中改变边框和颜色?

如何在TextInputLayout非焦点中改变边框和颜色?
EN

Stack Overflow用户
提问于 2020-02-05 15:34:34
回答 2查看 5.9K关注 0票数 2

如何在TextInputLayout非焦点中改变边框和颜色?

代码语言:javascript
运行
复制
<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_height="wrap_content"
        app:boxStrokeWidth="4dp"
        app:boxStrokeColor="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@id/a">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:hint="@string/app_name"
            android:textColorHint="@color/colorPrimary"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

我试着:

代码语言:javascript
运行
复制
app:boxStrokeWidth="4dp"
app:boxStrokeColor="@color/colorPrimary"

但是,不要在不集中的地方工作

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-06 11:01:43

如果希望在聚焦/非聚焦模式下具有不同的笔画宽度,则可以使用boxStrokeWidthboxStrokeWidthFocused属性。

代码语言:javascript
运行
复制
<!-- The value to use for the box's stroke when in outline box mode, 
     or for the underline stroke in filled mode. -->
<attr format="dimension" name="boxStrokeWidth"/>
<!-- The value to use for the focused box's stroke when in outline box mode,
     or for the focused underline stroke in filled mode.. -->
<attr format="dimension" name="boxStrokeWidthFocused"/>

类似于:

代码语言:javascript
运行
复制
    <com.google.android.material.textfield.TextInputLayout
        app:boxStrokeWidthFocused="4dp"
        app:boxStrokeWidth="1dp"
        app:boxStrokeColor="@color/text_input_layout_stroke_color"
        ..>

对于笔画颜色,可以定义如下所示的选择器:

代码语言:javascript
运行
复制
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_focused="true"/>
  <item android:alpha="0.87" android:color="@color/..." android:state_hovered="true"/>
  <item android:color="@color/.." android:state_enabled="false"/>
  <item android:color="@color/..."/> <!--unfocused-->
</selector>

注意: boxStrokeWidthFocused至少需要版本1.2.0-alpha04

票数 13
EN

Stack Overflow用户

发布于 2020-02-05 20:17:42

更改边框;使用绘图选择器作为TextInputLayout的背景。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorRipple">
    <item
        android:state_enabled="true"
        android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <stroke android:color="#aA0000" android:width="1dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#dddddd" />
            <stroke android:color="#ff00ff" android:width="1dp"/>
        </shape>
    </item>
</selector>

更改文本颜色;对TextInputEditText的文本颜色使用可绘制选择器

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#333333" android:state_selected="true" />
    <item android:color="#333333" android:state_focused="true" />
    <item android:color="#009900" /> <!-- default case -->
</selector>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60079180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档