[英]JavaFX: Apply text color to TableCell using custom style sheet?
[英]Cannot apply style for custom edit text
我正在嘗試以編程方式應用 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);
}
}
如您所見,在構造函數中,我將樣式設置為:
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>
使用您的應用程序的哪種風格? 只需添加到您的主要風格:
<item name="editTextStyle">@style/YourStyle</item>
例子:
<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
使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.