Android自定义编辑文本设置自定义默认样式(如果未提供)

Android自定义编辑文本设置自定义默认样式(如果未提供),android,Android,我已经创建了一个自定义视图(作为库提供),如果用户在项目中使用它时没有提供任何样式,我希望它使用默认样式 class SuggestionTextView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle ) : AppCompatEditText(cont

我已经创建了一个自定义视图(作为库提供),如果用户在项目中使用它时没有提供任何样式,我希望它使用默认样式

class SuggestionTextView
@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle
) : AppCompatEditText(context, attrs, defStyleAttr) 
此样式在我的模块中,如果开发人员不提供自己的样式,我希望使用它:

 <style name="Widget.AppCompat.SuggestionTextView" parent="Widget.AppCompat.EditText">
        <item name="imageTintColor">@color/myBlue</item>
        <item name="android:textColor">@color/myBlue</item>
        <item name="android:textColorHint">@color/myLightGray</item>
        <item name="android:background">@drawable/bg_white_border_gray</item>
        <item name="android:textAppearance">@style/SuggestionTextViewTextAppearance</item>
    </style>

    <style name="SuggestionTextViewTextAppearance" parent="TextAppearance.AppCompat">
        <item name="android:textSize">20sp</item>
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

@颜色/我的蓝色
@颜色/我的蓝色
@颜色/浅灰色
@可拉深/背景白色边框灰色
@样式/建议文本视图文本外观
20便士
无衬线灯
如果用户在使用“我的视图”时提供了自己的视图,我希望使用该视图,即:

<com.mypackage.suggestionscomponent.SuggestionTextView
            android:id="@+id/suggestionTextView"
            style="@style/Widget.AppCompat.UserCustomSuggestionTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textHeader" />


谢谢

找到了解决方案(谷歌真的很难做到)

  • 在attrs.xml上为自定义视图(仅限自定义属性)创建声明样式标签:
  • 在活动中使用自定义控件,使用在步骤3中创建的自定义样式:
  • 
    
  • 创建另一个样式以替代默认样式(如果需要)
  • 应用新样式:
  • 
    
    就这样:)

    <declare-styleable name="SuggestionTextView">
        <attr name="nResults" format="integer" />
        <attr name="language" format="string" />
        <attr name="imageTintColor" format="color" />
    </declare-styleable>
    
    <declare-styleable name="CustomTheme">
        <attr name="customSuggestionTextViewStyle" format="reference"/>
    </declare-styleable>
    
    <style name="Widget.AppCompat.SuggestionTextView" parent="Widget.AppCompat.EditText">
        <item name="imageTintColor">@color/myRed</item>
        <item name="android:textColor">@color/myBlue</item>
        <item name="android:background">@drawable/bg_white_border_gray</item>
        <item name="android:textColorHint">@color/myLightGray</item>
        <item name="android:textAppearance">@style/SuggestionTextViewTextAppearance</item>
    </style>
    
    <style name="SuggestionTextViewTextAppearance" parent="TextAppearance.AppCompat">
       <item name="android:textSize">33sp</item>
       <item name="android:fontFamily">sans-serif-light</item>
    </style>
    
    <style name="SuggestionTextViewTheme" parent="@android:style/Theme">
        <item name="customSuggestionTextViewStyle">@style/Widget.AppCompat.SuggestionTextView</item>
    </style>
    
    class SuggestionTextView
    @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = R.attr.customSuggestionTextViewStyle
    ) : AppCompatEditText(ContextThemeWrapper(context, R.style.SuggestionTextViewTheme), attrs, defStyleAttr) {
    
    init {
            context.theme.obtainStyledAttributes(
                attrs,
                R.styleable.SuggestionTextView,
                defStyleAttr, R.style.SuggestionTextViewTheme
            ).apply {
                try {
                    nResults = getInteger(R.styleable.SuggestionTextView_nResults, 3)
                    language = getString(R.styleable.SuggestionTextView_language) ?: "en"
                    imageTintColor = getColor(
                        R.styleable.SuggestionTextView_imageTintColor,
                        ContextCompat.getColor(context, R.color.myRed)
                    )
                } finally {
                    recycle()
                }
            }
    }
    
    <com.mypackage.suggestionscomponent.SuggestionTextView
        android:id="@+id/suggestionTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <com.mypackage.suggestionscomponent.SuggestionTextView
         style="@style/Widget.AppCompat.DifferentSuggestionTextView"
         android:id="@+id/suggestionTextView"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />