Android自定义类中设置自定义TextStyle及粗体失效问题求助
Alright, let's get to the bottom of why your text isn't showing up bold (or bold-italic) in your custom CustomTagGroup class. After looking through your code and style setup, there are three main issues preventing the text style from working correctly. Let's fix them step by step:
1. Missing Namespace Prefix in Your Style
First, in your style definition, you forgot to add the custom namespace prefix to your descriptionTextStyle attribute. Since your other custom attributes (like atg_horizontalPadding) use the atg prefix, your descriptionTextStyle needs it too—otherwise the system won't recognize it as your custom attribute and will fall back to the default value.
Fix the Style XML:
<style name="CustomTagGroup"> <item name="atg_horizontalPadding">12dp</item> <item name="atg_verticalPadding">3dp</item> <item name="android:textStyle">bold</item> <!-- Add the atg: prefix to your custom attribute --> <item name="atg:descriptionTextStyle">bolditalic</item> </style>
2. Not Applying the Text Style to Your TextView
You're correctly reading the descriptionTextStyle value from the TypedArray, but you never actually use that value to update your TextView's typeface. You need to convert the integer flag to a Typeface and apply it to the text view that displays your description. We'll also fix the missing TypedArray recycle, which prevents memory leaks.
Fix the Java Code:
public CustomTagGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { // Inflate your custom layout and get the description TextView LayoutInflater.from(context).inflate(R.layout.your_custom_tag_group_layout, this, true); TextView descriptionTextView = findViewById(R.id.your_description_text_id); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTagGroup, defStyleAttr, R.style.CustomTagGroup); try { // Get the text style flag (use Typeface.NORMAL as default if no value is set) int textStyleFlag = a.getInt(R.styleable.CustomTagGroup_descriptionTextStyle, Typeface.NORMAL); // Convert the flag to a Typeface object Typeface textTypeface = Typeface.defaultFromStyle(textStyleFlag); // Apply the Typeface to your TextView descriptionTextView.setTypeface(textTypeface); // Optional: Read and apply your padding attributes too int horizontalPadding = a.getDimensionPixelSize(R.styleable.CustomTagGroup_atg_horizontalPadding, 0); int verticalPadding = a.getDimensionPixelSize(R.styleable.CustomTagGroup_atg_verticalPadding, 0); descriptionTextView.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); } catch (Exception e) { e.printStackTrace(); } finally { // Critical: Recycle the TypedArray to avoid memory leaks a.recycle(); } }
3. Check for Overlapping Typeface Settings
Double-check if there's any other code that might be overwriting your TextView's typeface later on. For example:
- If you call
descriptionTextView.setTypeface()elsewhere in your code (like setting a custom font), it will override the style you set here. - If your TextView's layout XML has an
android:textStyleattribute, your code'ssetTypefacecall will still take precedence—so that's okay, but just be aware of it.
Bonus: Your Flag Values Are Correct!
Quick note: Your custom flag values match exactly with Android's Typeface constants:
normal= 0 →Typeface.NORMALbold= 1 →Typeface.BOLDitalic= 2 →Typeface.ITALICbolditalic= 3 →Typeface.BOLD_ITALIC
So usingTypeface.defaultFromStyle(textStyleFlag)works perfectly here—no extra conversion needed!
After making these changes, your text should show up with the bold (or bold-italic) style you set in your custom attribute.
内容的提问来源于stack exchange,提问作者Swati

