【问题标题】:Material Component is not able to change color材质组件无法更改颜色
【发布时间】:2019-04-17 20:30:10
【问题描述】:

请我尝试解决这个问题两天,但没有成功

我对新材料设计组件有疑问

我使用了样式 Widget.MaterialComponents.TextInputLayout.OutlinedBox

我尝试得到这个结果..(不改变主题颜色)只有 xml 或代码

我的实际小部件.. :(

我的主题

Theme.MaterialComponents.Light.NoActionBar.Bridge

组件的xml代码

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/core_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        style="@style/TextInputLayout"
        app:errorEnabled="true"
        android:layout_weight="9">

    <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            tools:hint="Trip name"/>

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

风格

<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="hintTextAppearance">@style/HintText</item>
</style>

<style name="HintText" parent="TextAppearance.MaterialComponents.Subtitle2">
    <item name="android:textColor">?secondaryVariant</item>
</style>

我尝试了互联网上的所有内容,但我不知道问题出在哪里。感谢您的帮助

在 gradle 中,我拥有所有最新版本的库和构建版本

如果你想补充更多信息,请写。

感谢您的帮助

【问题讨论】:

  • 您确定它会响应您的更改吗?颜色太差了,你还需要在拐角处添加更多半径
  • 是的,当不使用样式 HintText 时,“trip titpe”是白色的,请问你有一些 xmls,在哪里可以从 xml 更改配色方案?谢谢
  • 您可以覆盖主题,但您必须找到它使用的颜色和样式。
  • 我在主题中有颜色,但这输入了它的特殊主题。它无法通过 xml 或以编程方式更改?只有主题的颜色?我有自定义主题。

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


【解决方案1】:
<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="colorAccent">@color/HintText</item>
</style>

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/core_text"
        style="@style/TextInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="9"
        android:theme="@style/TextInputLayout"
        app:errorEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            tools:hint="Trip name" />
 </com.google.android.material.textfield.TextInputLayout>

另外如果你需要改变框的描边,你需要覆盖默认颜色

<color name="colorTextInputStroke">#CCD1D3</color>
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">
        @color/colorTextInputStroke
</color>

【讨论】:

【解决方案2】:

这个问题可以通过自定义主题中的这行来解决

   <item name="colorPrimary">?secondary</item>
   <item name="colorPrimaryDark">?secondaryVariant</item>
   <item name="colorOnPrimary">?onSecondaryH</item>
   <item name="colorSecondary">?primary</item>
   <item name="colorOnSecondary">?onPrimaryH</item>

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 1970-01-01
    • 2021-11-08
    • 2017-05-20
    • 2020-09-22
    • 1970-01-01
    • 2019-09-26
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多