【问题标题】:Cannot apply style for custom edit text无法为自定义编辑文本应用样式
【发布时间】:2020-05-12 02:32:57
【问题描述】:

我正在尝试以编程方式应用 TextInputLayout 主题来创建一次自定义编辑文本并在任何地方使用它。

这是我的自定义编辑文本类:

package com.enjoyapps.weddingapp.view;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import androidx.appcompat.view.ContextThemeWrapper;

import com.enjoyapps.weddingapp.R;
import com.google.android.material.textfield.TextInputLayout;

public class CustomEditText extends TextInputLayout {


    public CustomEditText(Context context) {
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox));
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
//        super(context, attrs);
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox), attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox), attrs, defStyleAttr);
        init();
    }

    private void init() {
        setBoxStrokeColor(Color.BLUE);
        setBoxCornerRadii(50,50,50,50);
        setBoxBackgroundColor(Color.BLUE);

    }
}

如您所见,在 Constructor 中我设置了样式:

new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)

当我将自定义编辑文本添加到 xml 时,它没有获得我在 init 方法中设置的属性。

但是当我在 xml 中应用相同的主题时,它正在工作并从 init 方法获取 all 属性。

<com.enjoyapps.weddingapp.view.CustomEditText
    android:layout_width="300dp"
    android:layout_centerInParent="true"
    android:layout_height="50dp">

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

</com.enjoyapps.weddingapp.view.CustomEditText>

【问题讨论】:

  • 如果某个答案对您有用,那么接受它很重要。它还会奖励你一些声望点!

标签: java android material-design


【解决方案1】:

您的应用使用哪种风格? 只需添加到您的主要风格:

&lt;item name="editTextStyle"&gt;@style/YourStyle&lt;/item&gt;

例子:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <!-- Customize your theme here. -->
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
   <item name="colorAccent">@color/colorAccent</item>

   <item name="editTextStyle">@style/YourStyle</item>
</style>

【讨论】:

  • 如果不是TextInputLayout,这将起作用,因为TextInputLayoutEditText 无关。需要的是textInputStyle 而不是editTextStyle。但是还有一个style,你必须申请TextInputEditText
【解决方案2】:

花了一些时间来找出问题所在。显然,TextInputLayout 使用 boxBackgroundMode 是为了使用来自不同样式的某些属性。

除了ContextThemeWrapper,您还必须为您的TextInputLayout 设置boxBackgroundMode

因此,如果您使用的是:

  • FilledBox :你需要使用BOX_BACKGROUND_FILLED
  • OutlinedBox :你需要使用BOX_BACKGROUND_OUTLINE

在您的情况下,只需将setBoxBackgroundMode(BOX_BACKGROUND_OUTLINE) 添加到您的init()

private void init() {
    // ... some styling here

    setBoxBackgroundMode(BOX_BACKGROUND_OUTLINE);
}

【讨论】:

    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2013-12-13
    相关资源
    最近更新 更多